blob: 58388c22d90c2f8a8eb8f65ed04d15b65ca3c10d [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 Pitrou011bd622009-10-20 21:52:47 +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 Pitrouf95a1b32010-05-09 15:52:27 +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);
Victor Stinnerb744ba12010-05-15 12:27:16 +000060static void initfsencoding(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000061static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000062static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000063static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000064static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000066static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static void err_input(perrdetail *);
69static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000070static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000071static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000072static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000073extern void _PyUnicode_Init(void);
74extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000075extern int _PyLong_Init(void);
76extern void PyLong_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000077
Mark Hammond8d98d2c2003-04-19 15:41:53 +000078#ifdef WITH_THREAD
79extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
80extern void _PyGILState_Fini(void);
81#endif /* WITH_THREAD */
82
Guido van Rossum82598051997-03-05 00:20:32 +000083int Py_DebugFlag; /* Needed by parser.c */
84int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000085int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumd8faa362007-04-27 19:54:29 +000086int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000087int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000088int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000089int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000090int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000091int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000092int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000093int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +000094int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095
Christian Heimes33fe8092008-04-13 13:53:33 +000096/* PyModule_GetWarningsModule is no longer necessary as of 2.6
97since _warnings is builtin. This API should not be used. */
98PyObject *
99PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000102}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000103
Guido van Rossum25ce5661997-08-02 03:10:38 +0000104static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000105
Thomas Wouters7e474022000-07-16 12:04:32 +0000106/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000107
108int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000109Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000112}
113
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114/* Global initializations. Can be undone by Py_Finalize(). Don't
115 call this twice without an intervening Py_Finalize() call. When
116 initializations fail, a fatal error is issued and the function does
117 not return. On return, the first thread and interpreter state have
118 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000119
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120 Locking: you must hold the interpreter lock while calling this.
121 (If the lock has not yet been initialized, that's equivalent to
122 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000123
Guido van Rossum25ce5661997-08-02 03:10:38 +0000124*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000125
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000126static int
127add_flag(int flag, const char *envs)
128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 int env = atoi(envs);
130 if (flag < env)
131 flag = env;
132 if (flag < 1)
133 flag = 1;
134 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000135}
136
Christian Heimes5833a2f2008-10-30 21:40:04 +0000137#if defined(HAVE_LANGINFO_H) && defined(CODESET)
138static char*
139get_codeset(void)
140{
Victor Stinner386fe712010-05-19 00:34:15 +0000141 char* codeset, *name_str;
142 PyObject *codec, *name = NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 codeset = nl_langinfo(CODESET);
145 if (!codeset || codeset[0] == '\0')
146 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 codec = _PyCodec_Lookup(codeset);
149 if (!codec)
150 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 name = PyObject_GetAttrString(codec, "name");
153 Py_CLEAR(codec);
154 if (!name)
155 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000156
Victor Stinner386fe712010-05-19 00:34:15 +0000157 name_str = _PyUnicode_AsString(name);
158 if (name == NULL)
159 goto error;
160 codeset = strdup(name_str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 Py_DECREF(name);
162 return codeset;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000163
164error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000166 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000168}
169#endif
170
Guido van Rossuma027efa1997-05-05 20:56:21 +0000171void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000172Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 PyInterpreterState *interp;
175 PyThreadState *tstate;
176 PyObject *bimod, *sysmod, *pstderr;
177 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 if (initialized)
181 return;
182 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000183
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000184#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 /* Set up the LC_CTYPE locale, so we can obtain
186 the locale's charset without having to switch
187 locales. */
188 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000189#endif
190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
192 Py_DebugFlag = add_flag(Py_DebugFlag, p);
193 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
194 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
195 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
196 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
197 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
198 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 interp = PyInterpreterState_New();
201 if (interp == NULL)
202 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 tstate = PyThreadState_New(interp);
205 if (tstate == NULL)
206 Py_FatalError("Py_Initialize: can't make first thread");
207 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (!_PyFrame_Init())
212 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (!_PyLong_Init())
215 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (!PyByteArray_Init())
218 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 interp->modules = PyDict_New();
223 if (interp->modules == NULL)
224 Py_FatalError("Py_Initialize: can't make modules dictionary");
225 interp->modules_reloading = PyDict_New();
226 if (interp->modules_reloading == NULL)
227 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 /* Init Unicode implementation; relies on the codec registry */
230 _PyUnicode_Init();
Guido van Rossumc94044c2000-03-10 23:03:54 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 bimod = _PyBuiltin_Init();
233 if (bimod == NULL)
234 Py_FatalError("Py_Initialize: can't initialize builtins modules");
235 _PyImport_FixupExtension(bimod, "builtins", "builtins");
236 interp->builtins = PyModule_GetDict(bimod);
237 if (interp->builtins == NULL)
238 Py_FatalError("Py_Initialize: can't initialize builtins dict");
239 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 /* initialize builtin exceptions */
242 _PyExc_Init();
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 sysmod = _PySys_Init();
245 if (sysmod == NULL)
246 Py_FatalError("Py_Initialize: can't initialize sys");
247 interp->sysdict = PyModule_GetDict(sysmod);
248 if (interp->sysdict == NULL)
249 Py_FatalError("Py_Initialize: can't initialize sys dict");
250 Py_INCREF(interp->sysdict);
251 _PyImport_FixupExtension(sysmod, "sys", "sys");
252 PySys_SetPath(Py_GetPath());
253 PyDict_SetItemString(interp->sysdict, "modules",
254 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* Set up a preliminary stderr printer until we have enough
257 infrastructure for the io module in place. */
258 pstderr = PyFile_NewStdPrinter(fileno(stderr));
259 if (pstderr == NULL)
260 Py_FatalError("Py_Initialize: can't set preliminary stderr");
261 PySys_SetObject("stderr", pstderr);
262 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000267
Victor Stinnerb744ba12010-05-15 12:27:16 +0000268 initfsencoding();
Martin v. Löwis011e8422009-05-05 04:43:17 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (install_sigs)
271 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 /* Initialize warnings. */
274 _PyWarnings_Init();
275 if (PySys_HasWarnOptions()) {
276 PyObject *warnings_module = PyImport_ImportModule("warnings");
277 if (!warnings_module)
278 PyErr_Clear();
279 Py_XDECREF(warnings_module);
280 }
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 initmain(); /* Module __main__ */
283 if (initstdio() < 0)
284 Py_FatalError(
285 "Py_Initialize: can't initialize sys standard streams");
286
287 /* auto-thread-state API, if available */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000288#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 _PyGILState_Init(interp, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000290#endif /* WITH_THREAD */
Victor Stinner52f6dd72010-03-12 14:45:56 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (!Py_NoSiteFlag)
293 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000294}
295
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000296void
297Py_Initialize(void)
298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000300}
301
302
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000303#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000305#endif
306
Guido van Rossume8432ac2007-07-09 15:04:50 +0000307/* Flush stdout and stderr */
308
Neal Norwitz2bad9702007-08-27 06:19:22 +0000309static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000310flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyObject *fout = PySys_GetObject("stdout");
313 PyObject *ferr = PySys_GetObject("stderr");
314 PyObject *tmp;
Guido van Rossume8432ac2007-07-09 15:04:50 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (fout != NULL && fout != Py_None) {
317 tmp = PyObject_CallMethod(fout, "flush", "");
318 if (tmp == NULL)
319 PyErr_Clear();
320 else
321 Py_DECREF(tmp);
322 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000323
Victor Stinner9467b212010-05-14 00:59:09 +0000324 if (ferr != NULL && ferr != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 tmp = PyObject_CallMethod(ferr, "flush", "");
326 if (tmp == NULL)
327 PyErr_Clear();
328 else
329 Py_DECREF(tmp);
330 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000331}
332
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333/* Undo the effect of Py_Initialize().
334
335 Beware: if multiple interpreter and/or thread states exist, these
336 are not wiped out; only the current thread and interpreter state
337 are deleted. But since everything else is deleted, those other
338 interpreter and thread states should no longer be used.
339
340 (XXX We should do better, e.g. wipe out all interpreters and
341 threads.)
342
343 Locking: as above.
344
345*/
346
347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 PyInterpreterState *interp;
351 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (!initialized)
354 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 /* The interpreter is still entirely intact at this point, and the
359 * exit funcs may be relying on that. In particular, if some thread
360 * or exit func is still waiting to do an import, the import machinery
361 * expects Py_IsInitialized() to return true. So don't say the
362 * interpreter is uninitialized until after the exit funcs have run.
363 * Note that Threading.py uses an exit func to do a join on all the
364 * threads created thru it, so this also protects pending imports in
365 * the threads created via Threading.
366 */
367 call_py_exitfuncs();
368 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* Flush stdout+stderr */
371 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* Get current thread state and interpreter pointer */
374 tstate = PyThreadState_GET();
375 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 /* Disable signal handling */
378 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* Clear type lookup cache */
381 PyType_ClearCache();
Christian Heimes26855632008-01-27 23:50:43 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* Collect garbage. This may call finalizers; it's nice to call these
384 * before all modules are destroyed.
385 * XXX If a __del__ or weakref callback is triggered here, and tries to
386 * XXX import a module, bad things can happen, because Python no
387 * XXX longer believes it's initialized.
388 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
389 * XXX is easy to provoke that way. I've also seen, e.g.,
390 * XXX Exception exceptions.ImportError: 'No module named sha'
391 * XXX in <function callback at 0x008F5718> ignored
392 * XXX but I'm unclear on exactly how that one happens. In any case,
393 * XXX I haven't seen a real-life report of either of these.
394 */
395 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000396#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* With COUNT_ALLOCS, it helps to run GC multiple times:
398 each collection might release some types from the type
399 list, so they become garbage. */
400 while (PyGC_Collect() > 0)
401 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* Destroy all modules */
405 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Flush stdout+stderr (again, in case more was printed) */
408 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* Collect final garbage. This disposes of cycles created by
411 * new-style class definitions, for example.
412 * XXX This is disabled because it caused too many problems. If
413 * XXX a __del__ or weakref callback triggers here, Python code has
414 * XXX a hard time running, because even the sys module has been
415 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
416 * XXX One symptom is a sequence of information-free messages
417 * XXX coming from threads (if a __del__ or callback is invoked,
418 * XXX other threads can execute too, and any exception they encounter
419 * XXX triggers a comedy of errors as subsystem after subsystem
420 * XXX fails to find what it *expects* to find in sys to help report
421 * XXX the exception and consequent unexpected failures). I've also
422 * XXX seen segfaults then, after adding print statements to the
423 * XXX Python code getting called.
424 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000425#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000427#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
430 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000433#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000435#endif
436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000438
Tim Peters9cf25ce2003-04-17 15:21:01 +0000439#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 /* Display all objects still alive -- this can invoke arbitrary
441 * __repr__ overrides, so requires a mostly-intact interpreter.
442 * Alas, a lot of stuff may still be alive now that will be cleaned
443 * up later.
444 */
445 if (Py_GETENV("PYTHONDUMPREFS"))
446 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000447#endif /* Py_TRACE_REFS */
448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* Clear interpreter state */
450 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 /* Now we decref the exception classes. After this point nothing
453 can raise an exception. That's okay, because each Fini() method
454 below has been checked to make sure no exceptions are ever
455 raised.
456 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* Cleanup auto-thread-state */
Christian Heimes7d2ff882007-11-30 14:35:04 +0000461#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 _PyGILState_Fini();
Christian Heimes7d2ff882007-11-30 14:35:04 +0000463#endif /* WITH_THREAD */
464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Delete current thread */
466 PyThreadState_Swap(NULL);
467 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 /* Sundry finalizers */
470 PyMethod_Fini();
471 PyFrame_Fini();
472 PyCFunction_Fini();
473 PyTuple_Fini();
474 PyList_Fini();
475 PySet_Fini();
476 PyBytes_Fini();
477 PyByteArray_Fini();
478 PyLong_Fini();
479 PyFloat_Fini();
480 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* Cleanup Unicode implementation */
483 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000486 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 free((char*)Py_FileSystemDefaultEncoding);
488 Py_FileSystemDefaultEncoding = NULL;
489 }
Christian Heimesc8967002007-11-30 10:18:26 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 /* XXX Still allocated:
492 - various static ad-hoc pointers to interned strings
493 - int and float free list blocks
494 - whatever various modules and libraries allocate
495 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000498
Tim Peters269b2a62003-04-17 19:52:29 +0000499#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 /* Display addresses (& refcnts) of all objects still alive.
501 * An address can be used to find the repr of the object, printed
502 * above by _Py_PrintReferences.
503 */
504 if (Py_GETENV("PYTHONDUMPREFS"))
505 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000506#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000507#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (Py_GETENV("PYTHONMALLOCSTATS"))
509 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000510#endif
511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000513}
514
515/* Create and initialize a new interpreter and thread, and return the
516 new thread. This requires that Py_Initialize() has been called
517 first.
518
519 Unsuccessful initialization yields a NULL pointer. Note that *no*
520 exception information is available even in this case -- the
521 exception information is held in the thread, and there is no
522 thread.
523
524 Locking: as above.
525
526*/
527
528PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000529Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyInterpreterState *interp;
532 PyThreadState *tstate, *save_tstate;
533 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (!initialized)
536 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 interp = PyInterpreterState_New();
539 if (interp == NULL)
540 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 tstate = PyThreadState_New(interp);
543 if (tstate == NULL) {
544 PyInterpreterState_Delete(interp);
545 return NULL;
546 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 interp->modules = PyDict_New();
553 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 bimod = _PyImport_FindExtension("builtins", "builtins");
556 if (bimod != NULL) {
557 interp->builtins = PyModule_GetDict(bimod);
558 if (interp->builtins == NULL)
559 goto handle_error;
560 Py_INCREF(interp->builtins);
561 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* initialize builtin exceptions */
564 _PyExc_Init();
Christian Heimes6a27efa2008-10-30 21:48:26 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 sysmod = _PyImport_FindExtension("sys", "sys");
567 if (bimod != NULL && sysmod != NULL) {
568 PyObject *pstderr;
569 interp->sysdict = PyModule_GetDict(sysmod);
570 if (interp->sysdict == NULL)
571 goto handle_error;
572 Py_INCREF(interp->sysdict);
573 PySys_SetPath(Py_GetPath());
574 PyDict_SetItemString(interp->sysdict, "modules",
575 interp->modules);
576 /* Set up a preliminary stderr printer until we have enough
577 infrastructure for the io module in place. */
578 pstderr = PyFile_NewStdPrinter(fileno(stderr));
579 if (pstderr == NULL)
580 Py_FatalError("Py_Initialize: can't set preliminary stderr");
581 PySys_SetObject("stderr", pstderr);
582 PySys_SetObject("__stderr__", pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 _PyImportHooks_Init();
585 if (initstdio() < 0)
586 Py_FatalError(
587 "Py_Initialize: can't initialize sys standard streams");
588 initmain();
589 if (!Py_NoSiteFlag)
590 initsite();
591 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (!PyErr_Occurred())
594 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595
Thomas Wouters89f507f2006-12-13 04:49:30 +0000596handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 PyErr_Print();
600 PyThreadState_Clear(tstate);
601 PyThreadState_Swap(save_tstate);
602 PyThreadState_Delete(tstate);
603 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606}
607
608/* Delete an interpreter and its last thread. This requires that the
609 given thread state is current, that the thread has no remaining
610 frames, and that it is its interpreter's only remaining thread.
611 It is a fatal error to violate these constraints.
612
613 (Py_Finalize() doesn't have these constraints -- it zaps
614 everything, regardless.)
615
616 Locking: as above.
617
618*/
619
620void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000621Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 if (tstate != PyThreadState_GET())
626 Py_FatalError("Py_EndInterpreter: thread is not current");
627 if (tstate->frame != NULL)
628 Py_FatalError("Py_EndInterpreter: thread still has a frame");
629 if (tstate != interp->tstate_head || tstate->next != NULL)
630 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 PyImport_Cleanup();
633 PyInterpreterState_Clear(interp);
634 PyThreadState_Swap(NULL);
635 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000636}
637
Martin v. Löwis790465f2008-04-05 20:41:37 +0000638static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000639
640void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000641Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (pn && *pn)
644 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000645}
646
Martin v. Löwis790465f2008-04-05 20:41:37 +0000647wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000651}
652
Martin v. Löwis790465f2008-04-05 20:41:37 +0000653static wchar_t *default_home = NULL;
654static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000655
656void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000657Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000660}
661
Martin v. Löwis790465f2008-04-05 20:41:37 +0000662wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000663Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 wchar_t *home = default_home;
666 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
667 char* chome = Py_GETENV("PYTHONHOME");
668 if (chome) {
669 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
670 if (r != (size_t)-1 && r <= PATH_MAX)
671 home = env_home;
672 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 }
675 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000676}
677
Guido van Rossum6135a871995-01-09 17:53:26 +0000678/* Create __main__ module */
679
680static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000681initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject *m, *d;
684 m = PyImport_AddModule("__main__");
685 if (m == NULL)
686 Py_FatalError("can't create __main__ module");
687 d = PyModule_GetDict(m);
688 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
689 PyObject *bimod = PyImport_ImportModule("builtins");
690 if (bimod == NULL ||
691 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
692 Py_FatalError("can't add __builtins__ to __main__");
693 Py_DECREF(bimod);
694 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000695}
696
Victor Stinnerb744ba12010-05-15 12:27:16 +0000697static void
698initfsencoding(void)
699{
700 PyObject *codec;
701#if defined(HAVE_LANGINFO_H) && defined(CODESET)
702 char *codeset;
703
704 /* On Unix, set the file system encoding according to the
705 user's preference, if the CODESET names a well-known
706 Python codec, and Py_FileSystemDefaultEncoding isn't
707 initialized by other means. Also set the encoding of
708 stdin and stdout if these are terminals. */
709 codeset = get_codeset();
710 if (codeset != NULL) {
711 Py_FileSystemDefaultEncoding = codeset;
712 Py_HasFileSystemDefaultEncoding = 0;
713 return;
714 }
715
716 PyErr_Clear();
717 fprintf(stderr,
718 "Unable to get the locale encoding: "
719 "fallback to utf-8\n");
720 Py_FileSystemDefaultEncoding = "utf-8";
721 Py_HasFileSystemDefaultEncoding = 1;
722#endif
723
724 /* the encoding is mbcs, utf-8 or ascii */
725 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
726 if (!codec) {
727 /* Such error can only occurs in critical situations: no more
728 * memory, import a module of the standard library failed,
729 * etc. */
730 Py_FatalError("Py_Initialize: unable to load the file system codec");
731 } else {
732 Py_DECREF(codec);
733 }
734}
735
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000736/* Import the site module (not into __main__ though) */
737
738static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000739initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PyObject *m;
742 m = PyImport_ImportModule("site");
743 if (m == NULL) {
744 PyErr_Print();
745 Py_Finalize();
746 exit(1);
747 }
748 else {
749 Py_DECREF(m);
750 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000751}
752
Antoine Pitrou05608432009-01-09 18:53:14 +0000753static PyObject*
754create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 int fd, int write_mode, char* name,
756 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
759 const char* mode;
760 PyObject *line_buffering;
761 int buffering, isatty;
Antoine Pitrou05608432009-01-09 18:53:14 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* stdin is always opened in buffered mode, first because it shouldn't
764 make a difference in common use cases, second because TextIOWrapper
765 depends on the presence of a read1() method which only exists on
766 buffered streams.
767 */
768 if (Py_UnbufferedStdioFlag && write_mode)
769 buffering = 0;
770 else
771 buffering = -1;
772 if (write_mode)
773 mode = "wb";
774 else
775 mode = "rb";
776 buf = PyObject_CallMethod(io, "open", "isiOOOi",
777 fd, mode, buffering,
778 Py_None, Py_None, Py_None, 0);
779 if (buf == NULL)
780 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (buffering) {
783 raw = PyObject_GetAttrString(buf, "raw");
784 if (raw == NULL)
785 goto error;
786 }
787 else {
788 raw = buf;
789 Py_INCREF(raw);
790 }
Antoine Pitrou05608432009-01-09 18:53:14 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 text = PyUnicode_FromString(name);
793 if (text == NULL || PyObject_SetAttrString(raw, "name", text) < 0)
794 goto error;
795 res = PyObject_CallMethod(raw, "isatty", "");
796 if (res == NULL)
797 goto error;
798 isatty = PyObject_IsTrue(res);
799 Py_DECREF(res);
800 if (isatty == -1)
801 goto error;
802 if (isatty || Py_UnbufferedStdioFlag)
803 line_buffering = Py_True;
804 else
805 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 Py_CLEAR(raw);
808 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 stream = PyObject_CallMethod(io, "TextIOWrapper", "OsssO",
811 buf, encoding, errors,
812 "\n", line_buffering);
813 Py_CLEAR(buf);
814 if (stream == NULL)
815 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (write_mode)
818 mode = "w";
819 else
820 mode = "r";
821 text = PyUnicode_FromString(mode);
822 if (!text || PyObject_SetAttrString(stream, "mode", text) < 0)
823 goto error;
824 Py_CLEAR(text);
825 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +0000826
827error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 Py_XDECREF(buf);
829 Py_XDECREF(stream);
830 Py_XDECREF(text);
831 Py_XDECREF(raw);
832 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +0000833}
834
Georg Brandl1a3284e2007-12-02 09:40:06 +0000835/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000836static int
837initstdio(void)
838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 PyObject *iomod = NULL, *wrapper;
840 PyObject *bimod = NULL;
841 PyObject *m;
842 PyObject *std = NULL;
843 int status = 0, fd;
844 PyObject * encoding_attr;
845 char *encoding = NULL, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 /* Hack to avoid a nasty recursion issue when Python is invoked
848 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
849 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
850 goto error;
851 }
852 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
855 goto error;
856 }
857 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 if (!(bimod = PyImport_ImportModule("builtins"))) {
860 goto error;
861 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 if (!(iomod = PyImport_ImportModule("io"))) {
864 goto error;
865 }
866 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
867 goto error;
868 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 /* Set builtins.open */
871 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
872 goto error;
873 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 encoding = Py_GETENV("PYTHONIOENCODING");
876 errors = NULL;
877 if (encoding) {
878 encoding = strdup(encoding);
879 errors = strchr(encoding, ':');
880 if (errors) {
881 *errors = '\0';
882 errors++;
883 }
884 }
Martin v. Löwis0f599892008-06-02 11:13:03 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 /* Set sys.stdin */
887 fd = fileno(stdin);
888 /* Under some conditions stdin, stdout and stderr may not be connected
889 * and fileno() may point to an invalid file descriptor. For example
890 * GUI apps don't have valid standard streams by default.
891 */
892 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000893#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 std = Py_None;
895 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000896#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000898#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 }
900 else {
901 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
902 if (std == NULL)
903 goto error;
904 } /* if (fd < 0) */
905 PySys_SetObject("__stdin__", std);
906 PySys_SetObject("stdin", std);
907 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* Set sys.stdout */
910 fd = fileno(stdout);
911 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000912#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 std = Py_None;
914 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000915#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000917#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 }
919 else {
920 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
921 if (std == NULL)
922 goto error;
923 } /* if (fd < 0) */
924 PySys_SetObject("__stdout__", std);
925 PySys_SetObject("stdout", std);
926 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000927
Guido van Rossum98297ee2007-11-06 21:34:58 +0000928#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* Set sys.stderr, replaces the preliminary stderr */
930 fd = fileno(stderr);
931 if (fd < 0) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000932#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 std = Py_None;
934 Py_INCREF(std);
Christian Heimes58cb1b82007-11-13 02:19:40 +0000935#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000937#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 }
939 else {
940 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
941 if (std == NULL)
942 goto error;
943 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* Same as hack above, pre-import stderr's codec to avoid recursion
946 when import.c tries to write to stderr in verbose mode. */
947 encoding_attr = PyObject_GetAttrString(std, "encoding");
948 if (encoding_attr != NULL) {
949 const char * encoding;
950 encoding = _PyUnicode_AsString(encoding_attr);
951 if (encoding != NULL) {
952 _PyCodec_Lookup(encoding);
953 }
954 }
955 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PySys_SetObject("__stderr__", std);
958 PySys_SetObject("stderr", std);
959 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000960#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000963 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 status = -1;
965 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 if (encoding)
968 free(encoding);
969 Py_XDECREF(bimod);
970 Py_XDECREF(iomod);
971 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000972}
973
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000974/* Parse input from a file and execute it */
975
976int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000977PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (filename == NULL)
981 filename = "???";
982 if (Py_FdIsInteractive(fp, filename)) {
983 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
984 if (closeit)
985 fclose(fp);
986 return err;
987 }
988 else
989 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000990}
991
992int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000993PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *v;
996 int ret;
997 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (flags == NULL) {
1000 flags = &local_flags;
1001 local_flags.cf_flags = 0;
1002 }
1003 v = PySys_GetObject("ps1");
1004 if (v == NULL) {
1005 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
1006 Py_XDECREF(v);
1007 }
1008 v = PySys_GetObject("ps2");
1009 if (v == NULL) {
1010 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
1011 Py_XDECREF(v);
1012 }
1013 for (;;) {
1014 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
1015 PRINT_TOTAL_REFS();
1016 if (ret == E_EOF)
1017 return 0;
1018 /*
1019 if (ret == E_NOMEM)
1020 return -1;
1021 */
1022 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001023}
1024
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001025/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001026static int PARSER_FLAGS(PyCompilerFlags *flags)
1027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 int parser_flags = 0;
1029 if (!flags)
1030 return 0;
1031 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1032 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1033 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1034 parser_flags |= PyPARSE_IGNORE_COOKIE;
1035 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1036 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1037 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001038}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001039
Thomas Wouters89f507f2006-12-13 04:49:30 +00001040#if 0
1041/* Keep an example of flags with future keyword support. */
1042#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1044 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1045 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1046 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001047#endif
1048
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001049int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001050PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 PyObject *m, *d, *v, *w, *oenc = NULL;
1053 mod_ty mod;
1054 PyArena *arena;
1055 char *ps1 = "", *ps2 = "", *enc = NULL;
1056 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (fp == stdin) {
1059 /* Fetch encoding from sys.stdin */
1060 v = PySys_GetObject("stdin");
1061 if (v == NULL || v == Py_None)
1062 return -1;
1063 oenc = PyObject_GetAttrString(v, "encoding");
1064 if (!oenc)
1065 return -1;
1066 enc = _PyUnicode_AsString(oenc);
Victor Stinner386fe712010-05-19 00:34:15 +00001067 if (enc == NULL)
1068 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 }
1070 v = PySys_GetObject("ps1");
1071 if (v != NULL) {
1072 v = PyObject_Str(v);
1073 if (v == NULL)
1074 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001075 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001077 if (ps1 == NULL) {
1078 PyErr_Clear();
1079 ps1 = "";
1080 }
1081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 }
1083 w = PySys_GetObject("ps2");
1084 if (w != NULL) {
1085 w = PyObject_Str(w);
1086 if (w == NULL)
1087 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001088 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001090 if (ps2 == NULL) {
1091 PyErr_Clear();
1092 ps2 = "";
1093 }
1094 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 }
1096 arena = PyArena_New();
1097 if (arena == NULL) {
1098 Py_XDECREF(v);
1099 Py_XDECREF(w);
1100 Py_XDECREF(oenc);
1101 return -1;
1102 }
1103 mod = PyParser_ASTFromFile(fp, filename, enc,
1104 Py_single_input, ps1, ps2,
1105 flags, &errcode, arena);
1106 Py_XDECREF(v);
1107 Py_XDECREF(w);
1108 Py_XDECREF(oenc);
1109 if (mod == NULL) {
1110 PyArena_Free(arena);
1111 if (errcode == E_EOF) {
1112 PyErr_Clear();
1113 return E_EOF;
1114 }
1115 PyErr_Print();
1116 return -1;
1117 }
1118 m = PyImport_AddModule("__main__");
1119 if (m == NULL) {
1120 PyArena_Free(arena);
1121 return -1;
1122 }
1123 d = PyModule_GetDict(m);
1124 v = run_mod(mod, filename, d, d, flags, arena);
1125 PyArena_Free(arena);
1126 flush_io();
1127 if (v == NULL) {
1128 PyErr_Print();
1129 return -1;
1130 }
1131 Py_DECREF(v);
1132 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001133}
1134
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001135/* Check whether a file maybe a pyc file: Look at the extension,
1136 the file type, and, if we may close it, at the first few bytes. */
1137
1138static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001139maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1142 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 /* Only look into the file if we are allowed to close it, since
1145 it then should also be seekable. */
1146 if (closeit) {
1147 /* Read only two bytes of the magic. If the file was opened in
1148 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1149 be read as they are on disk. */
1150 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1151 unsigned char buf[2];
1152 /* Mess: In case of -x, the stream is NOT at its start now,
1153 and ungetc() was used to push back the first newline,
1154 which makes the current stream position formally undefined,
1155 and a x-platform nightmare.
1156 Unfortunately, we have no direct way to know whether -x
1157 was specified. So we use a terrible hack: if the current
1158 stream position is not 0, we assume -x was specified, and
1159 give up. Bug 132850 on SourceForge spells out the
1160 hopelessness of trying anything else (fseek and ftell
1161 don't work predictably x-platform for text-mode files).
1162 */
1163 int ispyc = 0;
1164 if (ftell(fp) == 0) {
1165 if (fread(buf, 1, 2, fp) == 2 &&
1166 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1167 ispyc = 1;
1168 rewind(fp);
1169 }
1170 return ispyc;
1171 }
1172 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001173}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001174
Guido van Rossum0df002c2000-08-27 19:21:52 +00001175int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001176PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 PyObject *m, *d, *v;
1180 const char *ext;
1181 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 m = PyImport_AddModule("__main__");
1184 if (m == NULL)
1185 return -1;
1186 d = PyModule_GetDict(m);
1187 if (PyDict_GetItemString(d, "__file__") == NULL) {
1188 PyObject *f;
1189 f = PyUnicode_DecodeFSDefault(filename);
1190 if (f == NULL)
1191 return -1;
1192 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1193 Py_DECREF(f);
1194 return -1;
1195 }
1196 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0)
1197 return -1;
1198 set_file_name = 1;
1199 Py_DECREF(f);
1200 }
1201 len = strlen(filename);
1202 ext = filename + len - (len > 4 ? 4 : 0);
1203 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1204 /* Try to run a pyc file. First, re-open in binary */
1205 if (closeit)
1206 fclose(fp);
1207 if ((fp = fopen(filename, "rb")) == NULL) {
1208 fprintf(stderr, "python: Can't reopen .pyc file\n");
1209 ret = -1;
1210 goto done;
1211 }
1212 /* Turn on optimization if a .pyo file is given */
1213 if (strcmp(ext, ".pyo") == 0)
1214 Py_OptimizeFlag = 1;
1215 v = run_pyc_file(fp, filename, d, d, flags);
1216 } else {
1217 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1218 closeit, flags);
1219 }
1220 flush_io();
1221 if (v == NULL) {
1222 PyErr_Print();
1223 ret = -1;
1224 goto done;
1225 }
1226 Py_DECREF(v);
1227 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001228 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1230 PyErr_Clear();
1231 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001232}
1233
1234int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001235PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 PyObject *m, *d, *v;
1238 m = PyImport_AddModule("__main__");
1239 if (m == NULL)
1240 return -1;
1241 d = PyModule_GetDict(m);
1242 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1243 if (v == NULL) {
1244 PyErr_Print();
1245 return -1;
1246 }
1247 Py_DECREF(v);
1248 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001249}
1250
Barry Warsaw035574d1997-08-29 22:07:17 +00001251static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001252parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 long hold;
1256 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* old style errors */
1259 if (PyTuple_Check(err))
1260 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1261 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* new style errors. `err' is an instance */
Barry Warsaw035574d1997-08-29 22:07:17 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (! (v = PyObject_GetAttrString(err, "msg")))
1266 goto finally;
1267 *message = v;
Barry Warsaw035574d1997-08-29 22:07:17 +00001268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (!(v = PyObject_GetAttrString(err, "filename")))
1270 goto finally;
1271 if (v == Py_None)
1272 *filename = NULL;
1273 else if (! (*filename = _PyUnicode_AsString(v)))
1274 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 Py_DECREF(v);
1277 if (!(v = PyObject_GetAttrString(err, "lineno")))
1278 goto finally;
1279 hold = PyLong_AsLong(v);
1280 Py_DECREF(v);
1281 v = NULL;
1282 if (hold < 0 && PyErr_Occurred())
1283 goto finally;
1284 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 if (!(v = PyObject_GetAttrString(err, "offset")))
1287 goto finally;
1288 if (v == Py_None) {
1289 *offset = -1;
1290 Py_DECREF(v);
1291 v = NULL;
1292 } else {
1293 hold = PyLong_AsLong(v);
1294 Py_DECREF(v);
1295 v = NULL;
1296 if (hold < 0 && PyErr_Occurred())
1297 goto finally;
1298 *offset = (int)hold;
1299 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if (!(v = PyObject_GetAttrString(err, "text")))
1302 goto finally;
1303 if (v == Py_None)
1304 *text = NULL;
1305 else if (!PyUnicode_Check(v) ||
1306 !(*text = _PyUnicode_AsString(v)))
1307 goto finally;
1308 Py_DECREF(v);
1309 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001310
1311finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 Py_XDECREF(v);
1313 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001314}
1315
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001316void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001317PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001320}
1321
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001322static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001323print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 char *nl;
1326 if (offset >= 0) {
1327 if (offset > 0 && offset == (int)strlen(text))
1328 offset--;
1329 for (;;) {
1330 nl = strchr(text, '\n');
1331 if (nl == NULL || nl-text >= offset)
1332 break;
1333 offset -= (int)(nl+1-text);
1334 text = nl+1;
1335 }
1336 while (*text == ' ' || *text == '\t') {
1337 text++;
1338 offset--;
1339 }
1340 }
1341 PyFile_WriteString(" ", f);
1342 PyFile_WriteString(text, f);
1343 if (*text == '\0' || text[strlen(text)-1] != '\n')
1344 PyFile_WriteString("\n", f);
1345 if (offset == -1)
1346 return;
1347 PyFile_WriteString(" ", f);
1348 offset--;
1349 while (offset > 0) {
1350 PyFile_WriteString(" ", f);
1351 offset--;
1352 }
1353 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001354}
1355
Guido van Rossum66e8e862001-03-23 17:54:43 +00001356static void
1357handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 PyObject *exception, *value, *tb;
1360 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (Py_InspectFlag)
1363 /* Don't exit if -i flag was given. This flag is set to 0
1364 * when entering interactive mode for inspecting. */
1365 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 PyErr_Fetch(&exception, &value, &tb);
1368 fflush(stdout);
1369 if (value == NULL || value == Py_None)
1370 goto done;
1371 if (PyExceptionInstance_Check(value)) {
1372 /* The error code should be in the `code' attribute. */
1373 PyObject *code = PyObject_GetAttrString(value, "code");
1374 if (code) {
1375 Py_DECREF(value);
1376 value = code;
1377 if (value == Py_None)
1378 goto done;
1379 }
1380 /* If we failed to dig out the 'code' attribute,
1381 just let the else clause below print the error. */
1382 }
1383 if (PyLong_Check(value))
1384 exitcode = (int)PyLong_AsLong(value);
1385 else {
Victor Stinnere9fb3192010-05-17 08:58:51 +00001386 PyObject *sys_stderr = PySys_GetObject("stderr");
1387 if (sys_stderr != NULL)
1388 PyObject_CallMethod(sys_stderr, "flush", NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyObject_Print(value, stderr, Py_PRINT_RAW);
Victor Stinnere9fb3192010-05-17 08:58:51 +00001390 fflush(stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PySys_WriteStderr("\n");
1392 exitcode = 1;
1393 }
Tim Peterscf615b52003-04-19 18:47:02 +00001394 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 /* Restore and clear the exception info, in order to properly decref
1396 * the exception, value, and traceback. If we just exit instead,
1397 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1398 * some finalizers from running.
1399 */
1400 PyErr_Restore(exception, value, tb);
1401 PyErr_Clear();
1402 Py_Exit(exitcode);
1403 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001404}
1405
1406void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001407PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1412 handle_system_exit();
1413 }
1414 PyErr_Fetch(&exception, &v, &tb);
1415 if (exception == NULL)
1416 return;
1417 PyErr_NormalizeException(&exception, &v, &tb);
1418 if (tb == NULL) {
1419 tb = Py_None;
1420 Py_INCREF(tb);
1421 }
1422 PyException_SetTraceback(v, tb);
1423 if (exception == NULL)
1424 return;
1425 /* Now we know v != NULL too */
1426 if (set_sys_last_vars) {
1427 PySys_SetObject("last_type", exception);
1428 PySys_SetObject("last_value", v);
1429 PySys_SetObject("last_traceback", tb);
1430 }
1431 hook = PySys_GetObject("excepthook");
1432 if (hook) {
1433 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1434 PyObject *result = PyEval_CallObject(hook, args);
1435 if (result == NULL) {
1436 PyObject *exception2, *v2, *tb2;
1437 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1438 handle_system_exit();
1439 }
1440 PyErr_Fetch(&exception2, &v2, &tb2);
1441 PyErr_NormalizeException(&exception2, &v2, &tb2);
1442 /* It should not be possible for exception2 or v2
1443 to be NULL. However PyErr_Display() can't
1444 tolerate NULLs, so just be safe. */
1445 if (exception2 == NULL) {
1446 exception2 = Py_None;
1447 Py_INCREF(exception2);
1448 }
1449 if (v2 == NULL) {
1450 v2 = Py_None;
1451 Py_INCREF(v2);
1452 }
1453 fflush(stdout);
1454 PySys_WriteStderr("Error in sys.excepthook:\n");
1455 PyErr_Display(exception2, v2, tb2);
1456 PySys_WriteStderr("\nOriginal exception was:\n");
1457 PyErr_Display(exception, v, tb);
1458 Py_DECREF(exception2);
1459 Py_DECREF(v2);
1460 Py_XDECREF(tb2);
1461 }
1462 Py_XDECREF(result);
1463 Py_XDECREF(args);
1464 } else {
1465 PySys_WriteStderr("sys.excepthook is missing\n");
1466 PyErr_Display(exception, v, tb);
1467 }
1468 Py_XDECREF(exception);
1469 Py_XDECREF(v);
1470 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001471}
1472
Benjamin Petersone6528212008-07-15 15:32:09 +00001473static void
1474print_exception(PyObject *f, PyObject *value)
1475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 int err = 0;
1477 PyObject *type, *tb;
Benjamin Petersone6528212008-07-15 15:32:09 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 if (!PyExceptionInstance_Check(value)) {
1480 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1481 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1482 PyFile_WriteString(" found\n", f);
1483 return;
1484 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 Py_INCREF(value);
1487 fflush(stdout);
1488 type = (PyObject *) Py_TYPE(value);
1489 tb = PyException_GetTraceback(value);
1490 if (tb && tb != Py_None)
1491 err = PyTraceBack_Print(tb, f);
1492 if (err == 0 &&
1493 PyObject_HasAttrString(value, "print_file_and_line"))
1494 {
1495 PyObject *message;
1496 const char *filename, *text;
1497 int lineno, offset;
1498 if (!parse_syntax_error(value, &message, &filename,
1499 &lineno, &offset, &text))
1500 PyErr_Clear();
1501 else {
1502 char buf[10];
1503 PyFile_WriteString(" File \"", f);
1504 if (filename == NULL)
1505 PyFile_WriteString("<string>", f);
1506 else
1507 PyFile_WriteString(filename, f);
1508 PyFile_WriteString("\", line ", f);
1509 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1510 PyFile_WriteString(buf, f);
1511 PyFile_WriteString("\n", f);
1512 if (text != NULL)
1513 print_error_text(f, offset, text);
1514 Py_DECREF(value);
1515 value = message;
1516 /* Can't be bothered to check all those
1517 PyFile_WriteString() calls */
1518 if (PyErr_Occurred())
1519 err = -1;
1520 }
1521 }
1522 if (err) {
1523 /* Don't do anything else */
1524 }
1525 else {
1526 PyObject* moduleName;
1527 char* className;
1528 assert(PyExceptionClass_Check(type));
1529 className = PyExceptionClass_Name(type);
1530 if (className != NULL) {
1531 char *dot = strrchr(className, '.');
1532 if (dot != NULL)
1533 className = dot+1;
1534 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 moduleName = PyObject_GetAttrString(type, "__module__");
1537 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1538 {
1539 Py_DECREF(moduleName);
1540 err = PyFile_WriteString("<unknown>", f);
1541 }
1542 else {
1543 char* modstr = _PyUnicode_AsString(moduleName);
1544 if (modstr && strcmp(modstr, "builtins"))
1545 {
1546 err = PyFile_WriteString(modstr, f);
1547 err += PyFile_WriteString(".", f);
1548 }
1549 Py_DECREF(moduleName);
1550 }
1551 if (err == 0) {
1552 if (className == NULL)
1553 err = PyFile_WriteString("<unknown>", f);
1554 else
1555 err = PyFile_WriteString(className, f);
1556 }
1557 }
1558 if (err == 0 && (value != Py_None)) {
1559 PyObject *s = PyObject_Str(value);
1560 /* only print colon if the str() of the
1561 object is not the empty string
1562 */
1563 if (s == NULL)
1564 err = -1;
1565 else if (!PyUnicode_Check(s) ||
1566 PyUnicode_GetSize(s) != 0)
1567 err = PyFile_WriteString(": ", f);
1568 if (err == 0)
1569 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1570 Py_XDECREF(s);
1571 }
1572 /* try to write a newline in any case */
1573 err += PyFile_WriteString("\n", f);
1574 Py_XDECREF(tb);
1575 Py_DECREF(value);
1576 /* If an error happened here, don't show it.
1577 XXX This is wrong, but too many callers rely on this behavior. */
1578 if (err != 0)
1579 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001580}
1581
1582static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 "\nThe above exception was the direct cause "
1584 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001585
1586static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 "\nDuring handling of the above exception, "
1588 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00001589
1590static void
1591print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 int err = 0, res;
1594 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 if (seen != NULL) {
1597 /* Exception chaining */
1598 if (PySet_Add(seen, value) == -1)
1599 PyErr_Clear();
1600 else if (PyExceptionInstance_Check(value)) {
1601 cause = PyException_GetCause(value);
1602 context = PyException_GetContext(value);
1603 if (cause) {
1604 res = PySet_Contains(seen, cause);
1605 if (res == -1)
1606 PyErr_Clear();
1607 if (res == 0) {
1608 print_exception_recursive(
1609 f, cause, seen);
1610 err |= PyFile_WriteString(
1611 cause_message, f);
1612 }
1613 }
1614 else if (context) {
1615 res = PySet_Contains(seen, context);
1616 if (res == -1)
1617 PyErr_Clear();
1618 if (res == 0) {
1619 print_exception_recursive(
1620 f, context, seen);
1621 err |= PyFile_WriteString(
1622 context_message, f);
1623 }
1624 }
1625 Py_XDECREF(context);
1626 Py_XDECREF(cause);
1627 }
1628 }
1629 print_exception(f, value);
1630 if (err != 0)
1631 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00001632}
1633
Thomas Wouters477c8d52006-05-27 19:21:47 +00001634void
1635PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 PyObject *seen;
1638 PyObject *f = PySys_GetObject("stderr");
1639 if (f == Py_None) {
1640 /* pass */
1641 }
1642 else if (f == NULL) {
1643 _PyObject_Dump(value);
1644 fprintf(stderr, "lost sys.stderr\n");
1645 }
1646 else {
1647 /* We choose to ignore seen being possibly NULL, and report
1648 at least the main exception (it could be a MemoryError).
1649 */
1650 seen = PySet_New(NULL);
1651 if (seen == NULL)
1652 PyErr_Clear();
1653 print_exception_recursive(f, value, seen);
1654 Py_XDECREF(seen);
1655 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001656}
1657
Guido van Rossum82598051997-03-05 00:20:32 +00001658PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001659PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 PyObject *ret = NULL;
1663 mod_ty mod;
1664 PyArena *arena = PyArena_New();
1665 if (arena == NULL)
1666 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1669 if (mod != NULL)
1670 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1671 PyArena_Free(arena);
1672 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001673}
1674
1675PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001676PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 PyObject *ret;
1680 mod_ty mod;
1681 PyArena *arena = PyArena_New();
1682 if (arena == NULL)
1683 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1686 flags, NULL, arena);
1687 if (closeit)
1688 fclose(fp);
1689 if (mod == NULL) {
1690 PyArena_Free(arena);
1691 return NULL;
1692 }
1693 ret = run_mod(mod, filename, globals, locals, flags, arena);
1694 PyArena_Free(arena);
1695 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001696}
1697
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001698static void
1699flush_io(void)
1700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 PyObject *f, *r;
1702 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 /* Save the current exception */
1705 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 f = PySys_GetObject("stderr");
1708 if (f != NULL) {
1709 r = PyObject_CallMethod(f, "flush", "");
1710 if (r)
1711 Py_DECREF(r);
1712 else
1713 PyErr_Clear();
1714 }
1715 f = PySys_GetObject("stdout");
1716 if (f != NULL) {
1717 r = PyObject_CallMethod(f, "flush", "");
1718 if (r)
1719 Py_DECREF(r);
1720 else
1721 PyErr_Clear();
1722 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001725}
1726
Guido van Rossum82598051997-03-05 00:20:32 +00001727static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 PyCodeObject *co;
1732 PyObject *v;
1733 co = PyAST_Compile(mod, filename, flags, arena);
1734 if (co == NULL)
1735 return NULL;
1736 v = PyEval_EvalCode(co, globals, locals);
1737 Py_DECREF(co);
1738 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001739}
1740
Guido van Rossum82598051997-03-05 00:20:32 +00001741static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001742run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 PyCodeObject *co;
1746 PyObject *v;
1747 long magic;
1748 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 magic = PyMarshal_ReadLongFromFile(fp);
1751 if (magic != PyImport_GetMagicNumber()) {
1752 PyErr_SetString(PyExc_RuntimeError,
1753 "Bad magic number in .pyc file");
1754 return NULL;
1755 }
1756 (void) PyMarshal_ReadLongFromFile(fp);
1757 v = PyMarshal_ReadLastObjectFromFile(fp);
1758 fclose(fp);
1759 if (v == NULL || !PyCode_Check(v)) {
1760 Py_XDECREF(v);
1761 PyErr_SetString(PyExc_RuntimeError,
1762 "Bad code object in .pyc file");
1763 return NULL;
1764 }
1765 co = (PyCodeObject *)v;
1766 v = PyEval_EvalCode(co, globals, locals);
1767 if (v && flags)
1768 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1769 Py_DECREF(co);
1770 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001771}
1772
Guido van Rossum82598051997-03-05 00:20:32 +00001773PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001774Py_CompileStringFlags(const char *str, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 PyCodeObject *co;
1778 mod_ty mod;
1779 PyArena *arena = PyArena_New();
1780 if (arena == NULL)
1781 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1784 if (mod == NULL) {
1785 PyArena_Free(arena);
1786 return NULL;
1787 }
1788 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1789 PyObject *result = PyAST_mod2obj(mod);
1790 PyArena_Free(arena);
1791 return result;
1792 }
1793 co = PyAST_Compile(mod, filename, flags, arena);
1794 PyArena_Free(arena);
1795 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001796}
1797
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001798struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001799Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 struct symtable *st;
1802 mod_ty mod;
1803 PyCompilerFlags flags;
1804 PyArena *arena = PyArena_New();
1805 if (arena == NULL)
1806 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 flags.cf_flags = 0;
1809 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1810 if (mod == NULL) {
1811 PyArena_Free(arena);
1812 return NULL;
1813 }
1814 st = PySymtable_Build(mod, filename, 0);
1815 PyArena_Free(arena);
1816 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001817}
1818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819/* Preferred access to parser is through AST. */
1820mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001821PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 mod_ty mod;
1825 PyCompilerFlags localflags;
1826 perrdetail err;
1827 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1830 &_PyParser_Grammar, start, &err,
1831 &iflags);
1832 if (flags == NULL) {
1833 localflags.cf_flags = 0;
1834 flags = &localflags;
1835 }
1836 if (n) {
1837 flags->cf_flags |= iflags & PyCF_MASK;
1838 mod = PyAST_FromNode(n, flags, filename, arena);
1839 PyNode_Free(n);
1840 return mod;
1841 }
1842 else {
1843 err_input(&err);
1844 return NULL;
1845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846}
1847
1848mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001849PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 int start, char *ps1,
1851 char *ps2, PyCompilerFlags *flags, int *errcode,
1852 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 mod_ty mod;
1855 PyCompilerFlags localflags;
1856 perrdetail err;
1857 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1860 &_PyParser_Grammar,
1861 start, ps1, ps2, &err, &iflags);
1862 if (flags == NULL) {
1863 localflags.cf_flags = 0;
1864 flags = &localflags;
1865 }
1866 if (n) {
1867 flags->cf_flags |= iflags & PyCF_MASK;
1868 mod = PyAST_FromNode(n, flags, filename, arena);
1869 PyNode_Free(n);
1870 return mod;
1871 }
1872 else {
1873 err_input(&err);
1874 if (errcode)
1875 *errcode = err.error;
1876 return NULL;
1877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878}
1879
Guido van Rossuma110aa61994-08-29 12:50:44 +00001880/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001881
Guido van Rossuma110aa61994-08-29 12:50:44 +00001882node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001883PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 perrdetail err;
1886 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1887 &_PyParser_Grammar,
1888 start, NULL, NULL, &err, flags);
1889 if (n == NULL)
1890 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001893}
1894
Guido van Rossuma110aa61994-08-29 12:50:44 +00001895/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001896
Guido van Rossuma110aa61994-08-29 12:50:44 +00001897node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001898PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 perrdetail err;
1901 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1902 start, &err, flags);
1903 if (n == NULL)
1904 err_input(&err);
1905 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001906}
1907
1908node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001909PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001911{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 perrdetail err;
1913 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1914 &_PyParser_Grammar, start, &err, flags);
1915 if (n == NULL)
1916 err_input(&err);
1917 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001918}
1919
1920node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001921PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001922{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001924}
1925
Guido van Rossum66ebd912003-04-17 16:02:26 +00001926/* May want to move a more generalized form of this to parsetok.c or
1927 even parser modules. */
1928
1929void
1930PyParser_SetError(perrdetail *err)
1931{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001933}
1934
Guido van Rossuma110aa61994-08-29 12:50:44 +00001935/* Set the error appropriate to the given input error code (see errcode.h) */
1936
1937static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001938err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 PyObject *v, *w, *errtype, *errtext;
1941 PyObject *msg_obj = NULL;
1942 char *msg = NULL;
1943 errtype = PyExc_SyntaxError;
1944 switch (err->error) {
1945 case E_ERROR:
1946 return;
1947 case E_SYNTAX:
1948 errtype = PyExc_IndentationError;
1949 if (err->expected == INDENT)
1950 msg = "expected an indented block";
1951 else if (err->token == INDENT)
1952 msg = "unexpected indent";
1953 else if (err->token == DEDENT)
1954 msg = "unexpected unindent";
1955 else {
1956 errtype = PyExc_SyntaxError;
1957 msg = "invalid syntax";
1958 }
1959 break;
1960 case E_TOKEN:
1961 msg = "invalid token";
1962 break;
1963 case E_EOFS:
1964 msg = "EOF while scanning triple-quoted string literal";
1965 break;
1966 case E_EOLS:
1967 msg = "EOL while scanning string literal";
1968 break;
1969 case E_INTR:
1970 if (!PyErr_Occurred())
1971 PyErr_SetNone(PyExc_KeyboardInterrupt);
1972 goto cleanup;
1973 case E_NOMEM:
1974 PyErr_NoMemory();
1975 goto cleanup;
1976 case E_EOF:
1977 msg = "unexpected EOF while parsing";
1978 break;
1979 case E_TABSPACE:
1980 errtype = PyExc_TabError;
1981 msg = "inconsistent use of tabs and spaces in indentation";
1982 break;
1983 case E_OVERFLOW:
1984 msg = "expression too long";
1985 break;
1986 case E_DEDENT:
1987 errtype = PyExc_IndentationError;
1988 msg = "unindent does not match any outer indentation level";
1989 break;
1990 case E_TOODEEP:
1991 errtype = PyExc_IndentationError;
1992 msg = "too many levels of indentation";
1993 break;
1994 case E_DECODE: {
1995 PyObject *type, *value, *tb;
1996 PyErr_Fetch(&type, &value, &tb);
1997 msg = "unknown decode error";
1998 if (value != NULL)
1999 msg_obj = PyObject_Str(value);
2000 Py_XDECREF(type);
2001 Py_XDECREF(value);
2002 Py_XDECREF(tb);
2003 break;
2004 }
2005 case E_LINECONT:
2006 msg = "unexpected character after line continuation character";
2007 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 case E_IDENTIFIER:
2010 msg = "invalid character in identifier";
2011 break;
2012 default:
2013 fprintf(stderr, "error=%d\n", err->error);
2014 msg = "unknown parsing error";
2015 break;
2016 }
2017 /* err->text may not be UTF-8 in case of decoding errors.
2018 Explicitly convert to an object. */
2019 if (!err->text) {
2020 errtext = Py_None;
2021 Py_INCREF(Py_None);
2022 } else {
2023 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2024 "replace");
2025 }
2026 v = Py_BuildValue("(ziiN)", err->filename,
2027 err->lineno, err->offset, errtext);
2028 if (v != NULL) {
2029 if (msg_obj)
2030 w = Py_BuildValue("(OO)", msg_obj, v);
2031 else
2032 w = Py_BuildValue("(sO)", msg, v);
2033 } else
2034 w = NULL;
2035 Py_XDECREF(v);
2036 PyErr_SetObject(errtype, w);
2037 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002038cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 Py_XDECREF(msg_obj);
2040 if (err->text != NULL) {
2041 PyObject_FREE(err->text);
2042 err->text = NULL;
2043 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002044}
2045
2046/* Print fatal error message and abort */
2047
2048void
Tim Peters7c321a82002-07-09 02:57:01 +00002049Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 fprintf(stderr, "Fatal Python error: %s\n", msg);
2052 fflush(stderr); /* it helps in Windows debug build */
2053 if (PyErr_Occurred()) {
2054 PyErr_Print();
2055 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002056#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 {
2058 size_t len = strlen(msg);
2059 WCHAR* buffer;
2060 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 /* Convert the message to wchar_t. This uses a simple one-to-one
2063 conversion, assuming that the this error message actually uses ASCII
2064 only. If this ceases to be true, we will have to convert. */
2065 buffer = alloca( (len+1) * (sizeof *buffer));
2066 for( i=0; i<=len; ++i)
2067 buffer[i] = msg[i];
2068 OutputDebugStringW(L"Fatal Python error: ");
2069 OutputDebugStringW(buffer);
2070 OutputDebugStringW(L"\n");
2071 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002072#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002074#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002075#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002077}
2078
2079/* Clean up and exit */
2080
Guido van Rossuma110aa61994-08-29 12:50:44 +00002081#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002082#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002083#endif
2084
Collin Winter670e6922007-03-21 02:57:17 +00002085static void (*pyexitfunc)(void) = NULL;
2086/* For the atexit module. */
2087void _Py_PyAtExit(void (*func)(void))
2088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002090}
2091
2092static void
2093call_py_exitfuncs(void)
2094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 if (pyexitfunc == NULL)
2096 return;
Collin Winter670e6922007-03-21 02:57:17 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 (*pyexitfunc)();
2099 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002100}
2101
Antoine Pitrou011bd622009-10-20 21:52:47 +00002102/* Wait until threading._shutdown completes, provided
2103 the threading module was imported in the first place.
2104 The shutdown routine will wait until all non-daemon
2105 "threading" threads have completed. */
2106static void
2107wait_for_thread_shutdown(void)
2108{
2109#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 PyObject *result;
2111 PyThreadState *tstate = PyThreadState_GET();
2112 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2113 "threading");
2114 if (threading == NULL) {
2115 /* threading not imported */
2116 PyErr_Clear();
2117 return;
2118 }
2119 result = PyObject_CallMethod(threading, "_shutdown", "");
2120 if (result == NULL) {
2121 PyErr_WriteUnraisable(threading);
2122 }
2123 else {
2124 Py_DECREF(result);
2125 }
2126 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002127#endif
2128}
2129
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002130#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002131static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002132static int nexitfuncs = 0;
2133
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002134int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 if (nexitfuncs >= NEXITFUNCS)
2137 return -1;
2138 exitfuncs[nexitfuncs++] = func;
2139 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002140}
2141
Guido van Rossumcc283f51997-08-05 02:22:03 +00002142static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002143call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 while (nexitfuncs > 0)
2146 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 fflush(stdout);
2149 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002150}
2151
2152void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002153Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002158}
2159
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002160static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002161initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002162{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002163#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002165#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002166#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002168#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002169#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002171#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002173}
2174
Guido van Rossum7433b121997-02-14 19:45:36 +00002175
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002176/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2177 *
2178 * All of the code in this function must only use async-signal-safe functions,
2179 * listed at `man 7 signal` or
2180 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2181 */
2182void
2183_Py_RestoreSignals(void)
2184{
2185#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002187#endif
2188#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002190#endif
2191#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002193#endif
2194}
2195
2196
Guido van Rossum7433b121997-02-14 19:45:36 +00002197/*
2198 * The file descriptor fd is considered ``interactive'' if either
2199 * a) isatty(fd) is TRUE, or
2200 * b) the -i flag was given, and the filename associated with
2201 * the descriptor is NULL or "<stdin>" or "???".
2202 */
2203int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002204Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (isatty((int)fileno(fp)))
2207 return 1;
2208 if (!Py_InteractiveFlag)
2209 return 0;
2210 return (filename == NULL) ||
2211 (strcmp(filename, "<stdin>") == 0) ||
2212 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002213}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002214
2215
Tim Petersd08e3822003-04-17 15:24:21 +00002216#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002217#if defined(WIN32) && defined(_MSC_VER)
2218
2219/* Stack checking for Microsoft C */
2220
2221#include <malloc.h>
2222#include <excpt.h>
2223
Fred Drakee8de31c2000-08-31 05:38:39 +00002224/*
2225 * Return non-zero when we run out of memory on the stack; zero otherwise.
2226 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002227int
Fred Drake399739f2000-08-31 05:52:44 +00002228PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 __try {
2231 /* alloca throws a stack overflow exception if there's
2232 not enough space left on the stack */
2233 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2234 return 0;
2235 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2236 EXCEPTION_EXECUTE_HANDLER :
2237 EXCEPTION_CONTINUE_SEARCH) {
2238 int errcode = _resetstkoflw();
2239 if (errcode == 0)
2240 {
2241 Py_FatalError("Could not reset the stack!");
2242 }
2243 }
2244 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002245}
2246
2247#endif /* WIN32 && _MSC_VER */
2248
2249/* Alternate implementations can be added here... */
2250
2251#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002252
2253
2254/* Wrappers around sigaction() or signal(). */
2255
2256PyOS_sighandler_t
2257PyOS_getsig(int sig)
2258{
2259#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 struct sigaction context;
2261 if (sigaction(sig, NULL, &context) == -1)
2262 return SIG_ERR;
2263 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002264#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002266/* Special signal handling for the secure CRT in Visual Studio 2005 */
2267#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 switch (sig) {
2269 /* Only these signals are valid */
2270 case SIGINT:
2271 case SIGILL:
2272 case SIGFPE:
2273 case SIGSEGV:
2274 case SIGTERM:
2275 case SIGBREAK:
2276 case SIGABRT:
2277 break;
2278 /* Don't call signal() with other values or it will assert */
2279 default:
2280 return SIG_ERR;
2281 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002282#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 handler = signal(sig, SIG_IGN);
2284 if (handler != SIG_ERR)
2285 signal(sig, handler);
2286 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002287#endif
2288}
2289
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002290/*
2291 * All of the code in this function must only use async-signal-safe functions,
2292 * listed at `man 7 signal` or
2293 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2294 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002295PyOS_sighandler_t
2296PyOS_setsig(int sig, PyOS_sighandler_t handler)
2297{
2298#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 /* Some code in Modules/signalmodule.c depends on sigaction() being
2300 * used here if HAVE_SIGACTION is defined. Fix that if this code
2301 * changes to invalidate that assumption.
2302 */
2303 struct sigaction context, ocontext;
2304 context.sa_handler = handler;
2305 sigemptyset(&context.sa_mask);
2306 context.sa_flags = 0;
2307 if (sigaction(sig, &context, &ocontext) == -1)
2308 return SIG_ERR;
2309 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002310#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyOS_sighandler_t oldhandler;
2312 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002313#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002315#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002317#endif
2318}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319
2320/* Deprecated C API functions still provided for binary compatiblity */
2321
2322#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002323PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327}
2328
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002329#undef PyParser_SimpleParseString
2330PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331PyParser_SimpleParseString(const char *str, int start)
2332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002335
2336#undef PyRun_AnyFile
2337PyAPI_FUNC(int)
2338PyRun_AnyFile(FILE *fp, const char *name)
2339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002341}
2342
2343#undef PyRun_AnyFileEx
2344PyAPI_FUNC(int)
2345PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002348}
2349
2350#undef PyRun_AnyFileFlags
2351PyAPI_FUNC(int)
2352PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002355}
2356
2357#undef PyRun_File
2358PyAPI_FUNC(PyObject *)
2359PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002362}
2363
2364#undef PyRun_FileEx
2365PyAPI_FUNC(PyObject *)
2366PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369}
2370
2371#undef PyRun_FileFlags
2372PyAPI_FUNC(PyObject *)
2373PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002377}
2378
2379#undef PyRun_SimpleFile
2380PyAPI_FUNC(int)
2381PyRun_SimpleFile(FILE *f, const char *p)
2382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002384}
2385
2386#undef PyRun_SimpleFileEx
2387PyAPI_FUNC(int)
2388PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002391}
2392
2393
2394#undef PyRun_String
2395PyAPI_FUNC(PyObject *)
2396PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002399}
2400
2401#undef PyRun_SimpleString
2402PyAPI_FUNC(int)
2403PyRun_SimpleString(const char *s)
2404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002406}
2407
2408#undef Py_CompileString
2409PyAPI_FUNC(PyObject *)
2410Py_CompileString(const char *str, const char *p, int s)
2411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 return Py_CompileStringFlags(str, p, s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002413}
2414
2415#undef PyRun_InteractiveOne
2416PyAPI_FUNC(int)
2417PyRun_InteractiveOne(FILE *f, const char *p)
2418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002420}
2421
2422#undef PyRun_InteractiveLoop
2423PyAPI_FUNC(int)
2424PyRun_InteractiveLoop(FILE *f, const char *p)
2425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002427}
2428
2429#ifdef __cplusplus
2430}
2431#endif