blob: f2f14edb99eb8ff46e84c91e303054fb5b452799 [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"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000025
Martin v. Löwis73d538b2003-03-05 15:13:47 +000026#ifdef HAVE_LANGINFO_H
27#include <locale.h>
28#include <langinfo.h>
29#endif
30
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000031#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000032#undef BYTE
33#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000034#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000035#endif
36
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000039#else /* Py_REF_DEBUG */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040#define PRINT_TOTAL_REFS() fprintf(stderr, \
41 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
42 _Py_GetRefTotal())
43#endif
44
45#ifdef __cplusplus
46extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000047#endif
48
Martin v. Löwis790465f2008-04-05 20:41:37 +000049extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossum82598051997-03-05 00:20:32 +000051extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052
Guido van Rossumb73cc041993-11-01 16:28:59 +000053/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000054static void initmain(void);
55static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000056static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000057static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000059 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000060static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000061 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void err_input(perrdetail *);
63static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000064static void call_py_exitfuncs(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000065static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066extern void _PyUnicode_Init(void);
67extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000068extern int _PyLong_Init(void);
69extern void PyLong_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000070
Mark Hammond8d98d2c2003-04-19 15:41:53 +000071#ifdef WITH_THREAD
72extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73extern void _PyGILState_Fini(void);
74#endif /* WITH_THREAD */
75
Guido van Rossum82598051997-03-05 00:20:32 +000076int Py_DebugFlag; /* Needed by parser.c */
77int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000078int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumd8faa362007-04-27 19:54:29 +000079int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000080int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000081int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000082int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000083int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000084int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000085int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000086int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087
Christian Heimes33fe8092008-04-13 13:53:33 +000088/* PyModule_GetWarningsModule is no longer necessary as of 2.6
89since _warnings is builtin. This API should not be used. */
90PyObject *
91PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000092{
Christian Heimes33fe8092008-04-13 13:53:33 +000093 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000094}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000095
Guido van Rossum25ce5661997-08-02 03:10:38 +000096static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000097
Thomas Wouters7e474022000-07-16 12:04:32 +000098/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000099
100int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000102{
103 return initialized;
104}
105
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106/* Global initializations. Can be undone by Py_Finalize(). Don't
107 call this twice without an intervening Py_Finalize() call. When
108 initializations fail, a fatal error is issued and the function does
109 not return. On return, the first thread and interpreter state have
110 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000111
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112 Locking: you must hold the interpreter lock while calling this.
113 (If the lock has not yet been initialized, that's equivalent to
114 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000115
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000117
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000118static int
119add_flag(int flag, const char *envs)
120{
121 int env = atoi(envs);
122 if (flag < env)
123 flag = env;
124 if (flag < 1)
125 flag = 1;
126 return flag;
127}
128
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000130Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 PyThreadState *tstate;
Guido van Rossum826d8972007-10-30 18:34:07 +0000134 PyObject *bimod, *sysmod, *pstderr;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000135 char *p;
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000136#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000137 char *codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000138#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000139 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000140
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000141 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000142 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000143 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000144
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000145#ifdef HAVE_SETLOCALE
146 /* Set up the LC_CTYPE locale, so we can obtain
147 the locale's charset without having to switch
148 locales. */
149 setlocale(LC_CTYPE, "");
150#endif
151
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000152 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000153 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000154 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000155 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000156 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000157 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Christian Heimes790c8232008-01-07 21:14:23 +0000158 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
159 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000160
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161 interp = PyInterpreterState_New();
162 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000164
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165 tstate = PyThreadState_New(interp);
166 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 (void) PyThreadState_Swap(tstate);
169
Guido van Rossum70d893a2001-08-16 08:21:42 +0000170 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000171
Neal Norwitzb2501f42002-12-31 03:42:13 +0000172 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000173 Py_FatalError("Py_Initialize: can't init frames");
174
Guido van Rossumddefaf32007-01-14 03:31:43 +0000175 if (!_PyLong_Init())
176 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000177
Christian Heimes9c4756e2008-05-26 13:22:05 +0000178 if (!PyByteArray_Init())
Neal Norwitz6968b052007-02-27 19:02:19 +0000179 Py_FatalError("Py_Initialize: can't init bytes");
180
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000181 _PyFloat_Init();
182
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183 interp->modules = PyDict_New();
184 if (interp->modules == NULL)
185 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 interp->modules_reloading = PyDict_New();
187 if (interp->modules_reloading == NULL)
188 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189
Guido van Rossumc94044c2000-03-10 23:03:54 +0000190 /* Init Unicode implementation; relies on the codec registry */
191 _PyUnicode_Init();
192
Barry Warsawf242aa02000-05-25 23:09:49 +0000193 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194 if (bimod == NULL)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000195 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000196 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000197 if (interp->builtins == NULL)
198 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000199 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000201 /* initialize builtin exceptions */
202 _PyExc_Init();
203
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204 sysmod = _PySys_Init();
205 if (sysmod == NULL)
206 Py_FatalError("Py_Initialize: can't initialize sys");
207 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000208 if (interp->sysdict == NULL)
209 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210 Py_INCREF(interp->sysdict);
211 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000212 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 PyDict_SetItemString(interp->sysdict, "modules",
214 interp->modules);
215
Guido van Rossum826d8972007-10-30 18:34:07 +0000216 /* Set up a preliminary stderr printer until we have enough
217 infrastructure for the io module in place. */
218 pstderr = PyFile_NewStdPrinter(fileno(stderr));
219 if (pstderr == NULL)
220 Py_FatalError("Py_Initialize: can't set preliminary stderr");
221 PySys_SetObject("stderr", pstderr);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000222 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000223
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000224 _PyImport_Init();
225
Barry Warsaw035574d1997-08-29 22:07:17 +0000226 /* phase 2 of builtins */
Georg Brandl1a3284e2007-12-02 09:40:06 +0000227 _PyImport_FixupExtension("builtins", "builtins");
Barry Warsaw035574d1997-08-29 22:07:17 +0000228
Just van Rossum52e14d62002-12-30 22:08:05 +0000229 _PyImportHooks_Init();
230
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000231 if (install_sigs)
232 initsigs(); /* Signal handling stuff, including initintr() */
Christian Heimes33fe8092008-04-13 13:53:33 +0000233
234 /* Initialize warnings. */
235 _PyWarnings_Init();
236 if (PySys_HasWarnOptions()) {
237 PyObject *warnings_module = PyImport_ImportModule("warnings");
238 if (!warnings_module)
239 PyErr_Clear();
240 Py_XDECREF(warnings_module);
241 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242
243 initmain(); /* Module __main__ */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000244 if (!Py_NoSiteFlag)
245 initsite(); /* Module site */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000246 if (initstdio() < 0)
247 Py_FatalError(
248 "Py_Initialize: can't initialize sys standard streams");
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000249
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000250 /* auto-thread-state API, if available */
251#ifdef WITH_THREAD
252 _PyGILState_Init(interp, tstate);
253#endif /* WITH_THREAD */
254
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000255#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000256 /* On Unix, set the file system encoding according to the
257 user's preference, if the CODESET names a well-known
258 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000259 initialized by other means. Also set the encoding of
260 stdin and stdout if these are terminals. */
261
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000262 codeset = nl_langinfo(CODESET);
263 if (codeset && *codeset) {
264 PyObject *enc = PyCodec_Encoder(codeset);
265 if (enc) {
266 codeset = strdup(codeset);
267 Py_DECREF(enc);
268 } else {
269 codeset = NULL;
270 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000271 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000272 } else
273 codeset = NULL;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274
275 if (codeset) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000276 if (!Py_FileSystemDefaultEncoding)
277 Py_FileSystemDefaultEncoding = codeset;
278 else
279 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000280 }
281#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000282}
283
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000284void
285Py_Initialize(void)
286{
287 Py_InitializeEx(1);
288}
289
290
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000291#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000292extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000293#endif
294
Guido van Rossume8432ac2007-07-09 15:04:50 +0000295/* Flush stdout and stderr */
296
Neal Norwitz2bad9702007-08-27 06:19:22 +0000297static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000298flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000299{
300 PyObject *fout = PySys_GetObject("stdout");
301 PyObject *ferr = PySys_GetObject("stderr");
302 PyObject *tmp;
303
Christian Heimes2be03732007-11-15 02:26:46 +0000304 if (fout != NULL && fout != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000305 tmp = PyObject_CallMethod(fout, "flush", "");
306 if (tmp == NULL)
307 PyErr_Clear();
308 else
309 Py_DECREF(tmp);
310 }
311
Christian Heimes2be03732007-11-15 02:26:46 +0000312 if (ferr != NULL || ferr != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000313 tmp = PyObject_CallMethod(ferr, "flush", "");
314 if (tmp == NULL)
315 PyErr_Clear();
316 else
317 Py_DECREF(tmp);
318 }
319}
320
Guido van Rossum25ce5661997-08-02 03:10:38 +0000321/* Undo the effect of Py_Initialize().
322
323 Beware: if multiple interpreter and/or thread states exist, these
324 are not wiped out; only the current thread and interpreter state
325 are deleted. But since everything else is deleted, those other
326 interpreter and thread states should no longer be used.
327
328 (XXX We should do better, e.g. wipe out all interpreters and
329 threads.)
330
331 Locking: as above.
332
333*/
334
335void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000336Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000337{
338 PyInterpreterState *interp;
339 PyThreadState *tstate;
340
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000341 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000342 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000343
Tim Peters384fd102001-01-21 03:40:37 +0000344 /* The interpreter is still entirely intact at this point, and the
345 * exit funcs may be relying on that. In particular, if some thread
346 * or exit func is still waiting to do an import, the import machinery
347 * expects Py_IsInitialized() to return true. So don't say the
348 * interpreter is uninitialized until after the exit funcs have run.
349 * Note that Threading.py uses an exit func to do a join on all the
350 * threads created thru it, so this also protects pending imports in
351 * the threads created via Threading.
352 */
Collin Winter670e6922007-03-21 02:57:17 +0000353 call_py_exitfuncs();
Tim Peters384fd102001-01-21 03:40:37 +0000354 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000355
Guido van Rossume8432ac2007-07-09 15:04:50 +0000356 /* Flush stdout+stderr */
357 flush_std_files();
358
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000359 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000360 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000361 interp = tstate->interp;
362
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000363 /* Disable signal handling */
364 PyOS_FiniInterrupts();
365
Christian Heimes26855632008-01-27 23:50:43 +0000366 /* Clear type lookup cache */
367 PyType_ClearCache();
368
Guido van Rossume13ddc92003-04-17 17:29:22 +0000369 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000370 * before all modules are destroyed.
371 * XXX If a __del__ or weakref callback is triggered here, and tries to
372 * XXX import a module, bad things can happen, because Python no
373 * XXX longer believes it's initialized.
374 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
375 * XXX is easy to provoke that way. I've also seen, e.g.,
376 * XXX Exception exceptions.ImportError: 'No module named sha'
377 * XXX in <function callback at 0x008F5718> ignored
378 * XXX but I'm unclear on exactly how that one happens. In any case,
379 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000380 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000381 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382#ifdef COUNT_ALLOCS
383 /* With COUNT_ALLOCS, it helps to run GC multiple times:
384 each collection might release some types from the type
385 list, so they become garbage. */
386 while (PyGC_Collect() > 0)
387 /* nothing */;
388#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000389
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000390 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000392
Guido van Rossume8432ac2007-07-09 15:04:50 +0000393 /* Flush stdout+stderr (again, in case more was printed) */
394 flush_std_files();
395
Guido van Rossume13ddc92003-04-17 17:29:22 +0000396 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000397 * new-style class definitions, for example.
398 * XXX This is disabled because it caused too many problems. If
399 * XXX a __del__ or weakref callback triggers here, Python code has
400 * XXX a hard time running, because even the sys module has been
401 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
402 * XXX One symptom is a sequence of information-free messages
403 * XXX coming from threads (if a __del__ or callback is invoked,
404 * XXX other threads can execute too, and any exception they encounter
405 * XXX triggers a comedy of errors as subsystem after subsystem
406 * XXX fails to find what it *expects* to find in sys to help report
407 * XXX the exception and consequent unexpected failures). I've also
408 * XXX seen segfaults then, after adding print statements to the
409 * XXX Python code getting called.
410 */
411#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000412 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000413#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000414
Guido van Rossum1707aad1997-12-08 23:43:45 +0000415 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
416 _PyImport_Fini();
417
418 /* Debugging stuff */
419#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000421#endif
422
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000424
Tim Peters9cf25ce2003-04-17 15:21:01 +0000425#ifdef Py_TRACE_REFS
426 /* Display all objects still alive -- this can invoke arbitrary
427 * __repr__ overrides, so requires a mostly-intact interpreter.
428 * Alas, a lot of stuff may still be alive now that will be cleaned
429 * up later.
430 */
Tim Peters269b2a62003-04-17 19:52:29 +0000431 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000432 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000433#endif /* Py_TRACE_REFS */
434
Guido van Rossumd922fa42003-04-15 14:10:09 +0000435 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000436 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000437
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000438 /* Now we decref the exception classes. After this point nothing
439 can raise an exception. That's okay, because each Fini() method
440 below has been checked to make sure no exceptions are ever
441 raised.
442 */
443
444 _PyExc_Fini();
445
Christian Heimes7d2ff882007-11-30 14:35:04 +0000446 /* Cleanup auto-thread-state */
447#ifdef WITH_THREAD
448 _PyGILState_Fini();
449#endif /* WITH_THREAD */
450
Guido van Rossumd922fa42003-04-15 14:10:09 +0000451 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000452 PyThreadState_Swap(NULL);
453 PyInterpreterState_Delete(interp);
454
Guido van Rossumd922fa42003-04-15 14:10:09 +0000455 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000456 PyMethod_Fini();
457 PyFrame_Fini();
458 PyCFunction_Fini();
459 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000460 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000461 PySet_Fini();
Christian Heimes72b710a2008-05-26 13:28:38 +0000462 PyBytes_Fini();
Christian Heimes9c4756e2008-05-26 13:22:05 +0000463 PyByteArray_Fini();
Guido van Rossumddefaf32007-01-14 03:31:43 +0000464 PyLong_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000465 PyFloat_Fini();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000466 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000467
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000468 /* Cleanup Unicode implementation */
469 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000470
Christian Heimesc8967002007-11-30 10:18:26 +0000471 /* reset file system default encoding */
472 if (!Py_HasFileSystemDefaultEncoding) {
473 free((char*)Py_FileSystemDefaultEncoding);
Trent Nelson39e307e2008-03-19 06:45:48 +0000474 Py_FileSystemDefaultEncoding = NULL;
Christian Heimesc8967002007-11-30 10:18:26 +0000475 }
476
Guido van Rossumcc283f51997-08-05 02:22:03 +0000477 /* XXX Still allocated:
478 - various static ad-hoc pointers to interned strings
479 - int and float free list blocks
480 - whatever various modules and libraries allocate
481 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000482
483 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000484
Tim Peters269b2a62003-04-17 19:52:29 +0000485#ifdef Py_TRACE_REFS
486 /* Display addresses (& refcnts) of all objects still alive.
487 * An address can be used to find the repr of the object, printed
488 * above by _Py_PrintReferences.
489 */
490 if (Py_GETENV("PYTHONDUMPREFS"))
491 _Py_PrintReferenceAddresses(stderr);
492#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000493#ifdef PYMALLOC_DEBUG
494 if (Py_GETENV("PYTHONMALLOCSTATS"))
495 _PyObject_DebugMallocStats();
496#endif
497
Guido van Rossumcc283f51997-08-05 02:22:03 +0000498 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000499}
500
501/* Create and initialize a new interpreter and thread, and return the
502 new thread. This requires that Py_Initialize() has been called
503 first.
504
505 Unsuccessful initialization yields a NULL pointer. Note that *no*
506 exception information is available even in this case -- the
507 exception information is held in the thread, and there is no
508 thread.
509
510 Locking: as above.
511
512*/
513
514PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000515Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000516{
517 PyInterpreterState *interp;
518 PyThreadState *tstate, *save_tstate;
519 PyObject *bimod, *sysmod;
520
521 if (!initialized)
522 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
523
524 interp = PyInterpreterState_New();
525 if (interp == NULL)
526 return NULL;
527
528 tstate = PyThreadState_New(interp);
529 if (tstate == NULL) {
530 PyInterpreterState_Delete(interp);
531 return NULL;
532 }
533
534 save_tstate = PyThreadState_Swap(tstate);
535
536 /* XXX The following is lax in error checking */
537
538 interp->modules = PyDict_New();
Guido van Rossumd8faa362007-04-27 19:54:29 +0000539 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540
Georg Brandl1a3284e2007-12-02 09:40:06 +0000541 bimod = _PyImport_FindExtension("builtins", "builtins");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000542 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000543 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000544 if (interp->builtins == NULL)
545 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000546 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547 }
548 sysmod = _PyImport_FindExtension("sys", "sys");
549 if (bimod != NULL && sysmod != NULL) {
550 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000551 if (interp->sysdict == NULL)
552 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 Py_INCREF(interp->sysdict);
554 PySys_SetPath(Py_GetPath());
555 PyDict_SetItemString(interp->sysdict, "modules",
556 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000557 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000558 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000559 if (!Py_NoSiteFlag)
560 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561 }
562
563 if (!PyErr_Occurred())
564 return tstate;
565
Thomas Wouters89f507f2006-12-13 04:49:30 +0000566handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000567 /* Oops, it didn't work. Undo it all. */
568
569 PyErr_Print();
570 PyThreadState_Clear(tstate);
571 PyThreadState_Swap(save_tstate);
572 PyThreadState_Delete(tstate);
573 PyInterpreterState_Delete(interp);
574
575 return NULL;
576}
577
578/* Delete an interpreter and its last thread. This requires that the
579 given thread state is current, that the thread has no remaining
580 frames, and that it is its interpreter's only remaining thread.
581 It is a fatal error to violate these constraints.
582
583 (Py_Finalize() doesn't have these constraints -- it zaps
584 everything, regardless.)
585
586 Locking: as above.
587
588*/
589
590void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000591Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592{
593 PyInterpreterState *interp = tstate->interp;
594
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000595 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000596 Py_FatalError("Py_EndInterpreter: thread is not current");
597 if (tstate->frame != NULL)
598 Py_FatalError("Py_EndInterpreter: thread still has a frame");
599 if (tstate != interp->tstate_head || tstate->next != NULL)
600 Py_FatalError("Py_EndInterpreter: not the last thread");
601
602 PyImport_Cleanup();
603 PyInterpreterState_Clear(interp);
604 PyThreadState_Swap(NULL);
605 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000606}
607
Martin v. Löwis790465f2008-04-05 20:41:37 +0000608static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000609
610void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000611Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000612{
613 if (pn && *pn)
614 progname = pn;
615}
616
Martin v. Löwis790465f2008-04-05 20:41:37 +0000617wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000618Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000619{
620 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000621}
622
Martin v. Löwis790465f2008-04-05 20:41:37 +0000623static wchar_t *default_home = NULL;
624static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000625
626void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000627Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000628{
629 default_home = home;
630}
631
Martin v. Löwis790465f2008-04-05 20:41:37 +0000632wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000633Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000634{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000635 wchar_t *home = default_home;
636 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
637 char* chome = Py_GETENV("PYTHONHOME");
638 if (chome) {
639 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
640 if (r != (size_t)-1 && r <= PATH_MAX)
641 home = env_home;
642 }
643
644 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000645 return home;
646}
647
Guido van Rossum6135a871995-01-09 17:53:26 +0000648/* Create __main__ module */
649
650static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000652{
Guido van Rossum82598051997-03-05 00:20:32 +0000653 PyObject *m, *d;
654 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000655 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000656 Py_FatalError("can't create __main__ module");
657 d = PyModule_GetDict(m);
658 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Georg Brandl1a3284e2007-12-02 09:40:06 +0000659 PyObject *bimod = PyImport_ImportModule("builtins");
Guido van Rossum858cb731997-11-19 16:15:37 +0000660 if (bimod == NULL ||
661 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000662 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000663 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000664 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000665}
666
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000667/* Import the site module (not into __main__ though) */
668
669static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000670initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000671{
672 PyObject *m, *f;
673 m = PyImport_ImportModule("site");
674 if (m == NULL) {
675 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000676 if (f == NULL || f == Py_None)
677 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000678 if (Py_VerboseFlag) {
679 PyFile_WriteString(
680 "'import site' failed; traceback:\n", f);
681 PyErr_Print();
682 }
683 else {
684 PyFile_WriteString(
685 "'import site' failed; use -v for traceback\n", f);
686 PyErr_Clear();
687 }
688 }
689 else {
690 Py_DECREF(m);
691 }
692}
693
Georg Brandl1a3284e2007-12-02 09:40:06 +0000694/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000695static int
696initstdio(void)
697{
698 PyObject *iomod = NULL, *wrapper;
699 PyObject *bimod = NULL;
700 PyObject *m;
701 PyObject *std = NULL;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000702 int status = 0, fd;
Trent Nelson39e307e2008-03-19 06:45:48 +0000703 PyObject * encoding_attr;
Martin v. Löwis0f599892008-06-02 11:13:03 +0000704 char *encoding, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000705
706 /* Hack to avoid a nasty recursion issue when Python is invoked
707 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
708 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
709 goto error;
710 }
711 Py_DECREF(m);
712
713 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
714 goto error;
715 }
716 Py_DECREF(m);
717
Georg Brandl1a3284e2007-12-02 09:40:06 +0000718 if (!(bimod = PyImport_ImportModule("builtins"))) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000719 goto error;
720 }
721
722 if (!(iomod = PyImport_ImportModule("io"))) {
723 goto error;
724 }
725 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
726 goto error;
727 }
728
Georg Brandl1a3284e2007-12-02 09:40:06 +0000729 /* Set builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000730 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
731 goto error;
732 }
733
Martin v. Löwis0f599892008-06-02 11:13:03 +0000734 encoding = Py_GETENV("PYTHONIOENCODING");
735 if (encoding) {
736 encoding = strdup(encoding);
737 errors = strchr(encoding, ':');
738 if (errors) {
739 *errors = '\0';
740 errors++;
741 }
742 }
743
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000744 /* Set sys.stdin */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000745 fd = fileno(stdin);
746 /* Under some conditions stdin, stdout and stderr may not be connected
747 * and fileno() may point to an invalid file descriptor. For example
748 * GUI apps don't have valid standard streams by default.
749 */
750 if (fd < 0) {
751#ifdef MS_WINDOWS
752 std = Py_None;
753 Py_INCREF(std);
754#else
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000755 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000756#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000757 }
Christian Heimes58cb1b82007-11-13 02:19:40 +0000758 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000759 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding,
760 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000761 goto error;
762 }
763 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000764 PySys_SetObject("__stdin__", std);
765 PySys_SetObject("stdin", std);
766 Py_DECREF(std);
767
768 /* Set sys.stdout */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000769 fd = fileno(stdout);
770 if (fd < 0) {
771#ifdef MS_WINDOWS
772 std = Py_None;
773 Py_INCREF(std);
774#else
775 goto error;
776#endif
777 }
778 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000779 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding,
780 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000781 goto error;
782 }
783 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000784 PySys_SetObject("__stdout__", std);
785 PySys_SetObject("stdout", std);
786 Py_DECREF(std);
787
Guido van Rossum98297ee2007-11-06 21:34:58 +0000788#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000789 /* Set sys.stderr, replaces the preliminary stderr */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000790 fd = fileno(stderr);
791 if (fd < 0) {
792#ifdef MS_WINDOWS
793 std = Py_None;
794 Py_INCREF(std);
795#else
796 goto error;
797#endif
798 }
799 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000800 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
801 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000802 goto error;
803 }
804 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +0000805
806 /* Same as hack above, pre-import stderr's codec to avoid recursion
807 when import.c tries to write to stderr in verbose mode. */
808 encoding_attr = PyObject_GetAttrString(std, "encoding");
809 if (encoding_attr != NULL) {
810 const char * encoding;
811 encoding = PyUnicode_AsString(encoding_attr);
812 if (encoding != NULL) {
813 _PyCodec_Lookup(encoding);
814 }
815 }
816 PyErr_Clear(); /* Not a fatal error if codec isn't available */
817
Christian Heimesdb233082007-11-13 02:34:21 +0000818 PySys_SetObject("__stderr__", std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000819 PySys_SetObject("stderr", std);
820 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000821#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000822
Christian Heimes58cb1b82007-11-13 02:19:40 +0000823 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000824 error:
Christian Heimesdb233082007-11-13 02:34:21 +0000825 status = -1;
826 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000827
828 Py_XDECREF(bimod);
829 Py_XDECREF(iomod);
830 return status;
831}
832
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000833/* Parse input from a file and execute it */
834
835int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000836PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000837 PyCompilerFlags *flags)
838{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000839 if (filename == NULL)
840 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000841 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000842 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000843 if (closeit)
844 fclose(fp);
845 return err;
846 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000848 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000849}
850
851int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000852PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000853{
Guido van Rossum82598051997-03-05 00:20:32 +0000854 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000855 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000856 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000857
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000858 if (flags == NULL) {
859 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000860 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000861 }
Guido van Rossum82598051997-03-05 00:20:32 +0000862 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000863 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000864 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000865 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000866 }
Guido van Rossum82598051997-03-05 00:20:32 +0000867 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000868 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000869 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000870 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000871 }
872 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000873 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000875 if (ret == E_EOF)
876 return 0;
877 /*
878 if (ret == E_NOMEM)
879 return -1;
880 */
881 }
882}
883
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000884/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000885#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000886 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Guido van Rossum45aecf42006-03-15 04:58:47 +0000887 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000888
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889#if 0
890/* Keep an example of flags with future keyword support. */
891#define PARSER_FLAGS(flags) \
892 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
893 PyPARSE_DONT_IMPLY_DEDENT : 0) \
894 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
895 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
896#endif
897
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000898int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000899PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000900{
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000901 PyObject *m, *d, *v, *w, *oenc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000903 PyArena *arena;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000904 char *ps1 = "", *ps2 = "", *enc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000906
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000907 if (fp == stdin) {
908 /* Fetch encoding from sys.stdin */
909 v = PySys_GetObject("stdin");
Christian Heimes2be03732007-11-15 02:26:46 +0000910 if (v == NULL || v == Py_None)
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000911 return -1;
912 oenc = PyObject_GetAttrString(v, "encoding");
913 if (!oenc)
914 return -1;
915 enc = PyUnicode_AsString(oenc);
916 }
Guido van Rossum82598051997-03-05 00:20:32 +0000917 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000918 if (v != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000919 v = PyObject_Str(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000920 if (v == NULL)
921 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000922 else if (PyUnicode_Check(v))
923 ps1 = PyUnicode_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000924 }
Guido van Rossum82598051997-03-05 00:20:32 +0000925 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000926 if (w != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000927 w = PyObject_Str(w);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000928 if (w == NULL)
929 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000930 else if (PyUnicode_Check(w))
931 ps2 = PyUnicode_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000932 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000933 arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934 if (arena == NULL) {
935 Py_XDECREF(v);
936 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000937 Py_XDECREF(oenc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000938 return -1;
939 }
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000940 mod = PyParser_ASTFromFile(fp, filename, enc,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000942 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000943 Py_XDECREF(v);
944 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000945 Py_XDECREF(oenc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000947 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 if (errcode == E_EOF) {
949 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000950 return E_EOF;
951 }
Guido van Rossum82598051997-03-05 00:20:32 +0000952 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954 }
Guido van Rossum82598051997-03-05 00:20:32 +0000955 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000956 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000957 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000958 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000959 }
Guido van Rossum82598051997-03-05 00:20:32 +0000960 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000961 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000962 PyArena_Free(arena);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +0000963 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000965 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000966 return -1;
967 }
Guido van Rossum82598051997-03-05 00:20:32 +0000968 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000969 return 0;
970}
971
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000972/* Check whether a file maybe a pyc file: Look at the extension,
973 the file type, and, if we may close it, at the first few bytes. */
974
975static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000976maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000977{
978 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
979 return 1;
980
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000981 /* Only look into the file if we are allowed to close it, since
982 it then should also be seekable. */
983 if (closeit) {
984 /* Read only two bytes of the magic. If the file was opened in
985 text mode, the bytes 3 and 4 of the magic (\r\n) might not
986 be read as they are on disk. */
987 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
988 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000989 /* Mess: In case of -x, the stream is NOT at its start now,
990 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000991 which makes the current stream position formally undefined,
992 and a x-platform nightmare.
993 Unfortunately, we have no direct way to know whether -x
994 was specified. So we use a terrible hack: if the current
995 stream position is not 0, we assume -x was specified, and
996 give up. Bug 132850 on SourceForge spells out the
997 hopelessness of trying anything else (fseek and ftell
998 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000999 */
Tim Peters3e876562001-02-11 04:35:39 +00001000 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001001 if (ftell(fp) == 0) {
1002 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +00001003 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001004 ispyc = 1;
1005 rewind(fp);
1006 }
Tim Peters3e876562001-02-11 04:35:39 +00001007 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001008 }
1009 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001010}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001011
Guido van Rossum0df002c2000-08-27 19:21:52 +00001012int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001013PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001014 PyCompilerFlags *flags)
1015{
Guido van Rossum82598051997-03-05 00:20:32 +00001016 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001017 const char *ext;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001018 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001019
Guido van Rossum82598051997-03-05 00:20:32 +00001020 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001021 if (m == NULL)
1022 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001023 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +00001024 if (PyDict_GetItemString(d, "__file__") == NULL) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001025 PyObject *f;
1026 f = PyUnicode_DecodeFSDefault(filename);
Fred Drake8ed02042002-10-17 21:24:58 +00001027 if (f == NULL)
1028 return -1;
1029 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1030 Py_DECREF(f);
1031 return -1;
1032 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001033 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +00001034 Py_DECREF(f);
1035 }
Guido van Rossumfdef2711994-09-14 13:31:04 +00001036 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001037 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001038 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +00001039 if (closeit)
1040 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +00001041 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001042 fprintf(stderr, "python: Can't reopen .pyc file\n");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001043 ret = -1;
1044 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001045 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +00001046 /* Turn on optimization if a .pyo file is given */
1047 if (strcmp(ext, ".pyo") == 0)
1048 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001049 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001050 } else {
Tim Petersd08e3822003-04-17 15:24:21 +00001051 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001052 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001053 }
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +00001054 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001055 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001056 PyErr_Print();
Guido van Rossumd8faa362007-04-27 19:54:29 +00001057 ret = -1;
1058 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001059 }
Guido van Rossum82598051997-03-05 00:20:32 +00001060 Py_DECREF(v);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001061 ret = 0;
1062 done:
1063 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1064 PyErr_Clear();
1065 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001066}
1067
1068int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001069PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001070{
Guido van Rossum82598051997-03-05 00:20:32 +00001071 PyObject *m, *d, *v;
1072 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001073 if (m == NULL)
1074 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001075 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +00001076 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001077 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001078 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001079 return -1;
1080 }
Guido van Rossum82598051997-03-05 00:20:32 +00001081 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001082 return 0;
1083}
1084
Barry Warsaw035574d1997-08-29 22:07:17 +00001085static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001086parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1087 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001088{
1089 long hold;
1090 PyObject *v;
1091
1092 /* old style errors */
1093 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +00001094 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001095 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001096
1097 /* new style errors. `err' is an instance */
1098
1099 if (! (v = PyObject_GetAttrString(err, "msg")))
1100 goto finally;
1101 *message = v;
1102
1103 if (!(v = PyObject_GetAttrString(err, "filename")))
1104 goto finally;
1105 if (v == Py_None)
1106 *filename = NULL;
Kurt B. Kaiser43b15092008-01-05 04:32:22 +00001107 else if (! (*filename = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001108 goto finally;
1109
1110 Py_DECREF(v);
1111 if (!(v = PyObject_GetAttrString(err, "lineno")))
1112 goto finally;
Christian Heimes217cfd12007-12-02 14:31:20 +00001113 hold = PyLong_AsLong(v);
Barry Warsaw035574d1997-08-29 22:07:17 +00001114 Py_DECREF(v);
1115 v = NULL;
1116 if (hold < 0 && PyErr_Occurred())
1117 goto finally;
1118 *lineno = (int)hold;
1119
1120 if (!(v = PyObject_GetAttrString(err, "offset")))
1121 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001122 if (v == Py_None) {
1123 *offset = -1;
1124 Py_DECREF(v);
1125 v = NULL;
1126 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001127 hold = PyLong_AsLong(v);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001128 Py_DECREF(v);
1129 v = NULL;
1130 if (hold < 0 && PyErr_Occurred())
1131 goto finally;
1132 *offset = (int)hold;
1133 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001134
1135 if (!(v = PyObject_GetAttrString(err, "text")))
1136 goto finally;
1137 if (v == Py_None)
1138 *text = NULL;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001139 else if (!PyUnicode_Check(v) ||
1140 !(*text = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001141 goto finally;
1142 Py_DECREF(v);
1143 return 1;
1144
1145finally:
1146 Py_XDECREF(v);
1147 return 0;
1148}
1149
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001150void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001151PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001152{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001153 PyErr_PrintEx(1);
1154}
1155
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001156static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001157print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001158{
1159 char *nl;
1160 if (offset >= 0) {
1161 if (offset > 0 && offset == (int)strlen(text))
1162 offset--;
1163 for (;;) {
1164 nl = strchr(text, '\n');
1165 if (nl == NULL || nl-text >= offset)
1166 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001167 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001168 text = nl+1;
1169 }
1170 while (*text == ' ' || *text == '\t') {
1171 text++;
1172 offset--;
1173 }
1174 }
1175 PyFile_WriteString(" ", f);
1176 PyFile_WriteString(text, f);
1177 if (*text == '\0' || text[strlen(text)-1] != '\n')
1178 PyFile_WriteString("\n", f);
1179 if (offset == -1)
1180 return;
1181 PyFile_WriteString(" ", f);
1182 offset--;
1183 while (offset > 0) {
1184 PyFile_WriteString(" ", f);
1185 offset--;
1186 }
1187 PyFile_WriteString("^\n", f);
1188}
1189
Guido van Rossum66e8e862001-03-23 17:54:43 +00001190static void
1191handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001192{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001193 PyObject *exception, *value, *tb;
1194 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001195
Guido van Rossumd8faa362007-04-27 19:54:29 +00001196 if (Py_InspectFlag)
1197 /* Don't exit if -i flag was given. This flag is set to 0
1198 * when entering interactive mode for inspecting. */
1199 return;
1200
Guido van Rossum66e8e862001-03-23 17:54:43 +00001201 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001202 fflush(stdout);
1203 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001204 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001205 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001206 /* The error code should be in the `code' attribute. */
1207 PyObject *code = PyObject_GetAttrString(value, "code");
1208 if (code) {
1209 Py_DECREF(value);
1210 value = code;
1211 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001212 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001213 }
1214 /* If we failed to dig out the 'code' attribute,
1215 just let the else clause below print the error. */
1216 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001217 if (PyLong_Check(value))
1218 exitcode = (int)PyLong_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001219 else {
1220 PyObject_Print(value, stderr, Py_PRINT_RAW);
1221 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001222 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001223 }
Tim Peterscf615b52003-04-19 18:47:02 +00001224 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001225 /* Restore and clear the exception info, in order to properly decref
1226 * the exception, value, and traceback. If we just exit instead,
1227 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1228 * some finalizers from running.
1229 */
Tim Peterscf615b52003-04-19 18:47:02 +00001230 PyErr_Restore(exception, value, tb);
1231 PyErr_Clear();
1232 Py_Exit(exitcode);
1233 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001234}
1235
1236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001237PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001238{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001239 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001240
1241 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1242 handle_system_exit();
1243 }
Guido van Rossum82598051997-03-05 00:20:32 +00001244 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 if (exception == NULL)
1246 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001247 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001248 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001249 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001250 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001251 if (set_sys_last_vars) {
1252 PySys_SetObject("last_type", exception);
1253 PySys_SetObject("last_value", v);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001254 PySys_SetObject("last_traceback", tb ? tb : Py_None);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001255 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001256 hook = PySys_GetObject("excepthook");
1257 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001258 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001259 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001260 PyObject *result = PyEval_CallObject(hook, args);
1261 if (result == NULL) {
1262 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001263 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1264 handle_system_exit();
1265 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001266 PyErr_Fetch(&exception2, &v2, &tb2);
1267 PyErr_NormalizeException(&exception2, &v2, &tb2);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001268 /* It should not be possible for exception2 or v2
1269 to be NULL. However PyErr_Display() can't
1270 tolerate NULLs, so just be safe. */
1271 if (exception2 == NULL) {
1272 exception2 = Py_None;
1273 Py_INCREF(exception2);
1274 }
1275 if (v2 == NULL) {
1276 v2 = Py_None;
1277 Py_INCREF(v2);
1278 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001279 fflush(stdout);
1280 PySys_WriteStderr("Error in sys.excepthook:\n");
1281 PyErr_Display(exception2, v2, tb2);
1282 PySys_WriteStderr("\nOriginal exception was:\n");
1283 PyErr_Display(exception, v, tb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001284 Py_DECREF(exception2);
1285 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001286 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001287 }
1288 Py_XDECREF(result);
1289 Py_XDECREF(args);
1290 } else {
1291 PySys_WriteStderr("sys.excepthook is missing\n");
1292 PyErr_Display(exception, v, tb);
1293 }
1294 Py_XDECREF(exception);
1295 Py_XDECREF(v);
1296 Py_XDECREF(tb);
1297}
1298
Thomas Wouters477c8d52006-05-27 19:21:47 +00001299void
1300PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001301{
1302 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001303 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001304 Py_INCREF(value);
Christian Heimes2be03732007-11-15 02:26:46 +00001305 if (f == Py_None) {
1306 /* pass */
1307 }
1308 else if (f == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001309 _PyObject_Dump(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001310 fprintf(stderr, "lost sys.stderr\n");
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001311 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001312 else {
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001313 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001314 if (tb && tb != Py_None)
1315 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001316 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001317 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001318 {
Guido van Rossum82598051997-03-05 00:20:32 +00001319 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001320 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001321 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001322 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001323 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001324 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001325 else {
1326 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001327 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001328 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001329 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001330 else
Guido van Rossum82598051997-03-05 00:20:32 +00001331 PyFile_WriteString(filename, f);
1332 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001333 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001334 PyFile_WriteString(buf, f);
1335 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001336 if (text != NULL)
1337 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001338 Py_DECREF(value);
1339 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001340 /* Can't be bothered to check all those
1341 PyFile_WriteString() calls */
1342 if (PyErr_Occurred())
1343 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001344 }
1345 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001346 if (err) {
1347 /* Don't do anything else */
1348 }
Brett Cannonbf364092006-03-01 04:25:17 +00001349 else if (PyExceptionClass_Check(exception)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001350 PyObject* moduleName;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001351 char* className = PyExceptionClass_Name(exception);
1352 if (className != NULL) {
1353 char *dot = strrchr(className, '.');
1354 if (dot != NULL)
1355 className = dot+1;
1356 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001357
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001358 moduleName = PyObject_GetAttrString(exception, "__module__");
Guido van Rossumc5724de2007-10-10 21:38:59 +00001359 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1360 {
1361 Py_DECREF(moduleName);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001362 err = PyFile_WriteString("<unknown>", f);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001363 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001364 else {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001365 char* modstr = PyUnicode_AsString(moduleName);
Georg Brandl1a3284e2007-12-02 09:40:06 +00001366 if (modstr && strcmp(modstr, "builtins"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001367 {
1368 err = PyFile_WriteString(modstr, f);
1369 err += PyFile_WriteString(".", f);
1370 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001371 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001372 }
1373 if (err == 0) {
1374 if (className == NULL)
1375 err = PyFile_WriteString("<unknown>", f);
1376 else
Brett Cannonbf364092006-03-01 04:25:17 +00001377 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001378 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001379 }
1380 else
1381 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001382 if (err == 0 && (value != Py_None)) {
Thomas Heller519a0422007-11-15 20:48:54 +00001383 PyObject *s = PyObject_Str(value);
Brett Cannon2e63b732006-03-02 18:34:57 +00001384 /* only print colon if the str() of the
1385 object is not the empty string
1386 */
1387 if (s == NULL)
1388 err = -1;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001389 else if (!PyUnicode_Check(s) ||
1390 PyUnicode_GetSize(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001391 err = PyFile_WriteString(": ", f);
1392 if (err == 0)
1393 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1394 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001395 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001396 /* try to write a newline in any case */
1397 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001398 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001399 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001400 /* If an error happened here, don't show it.
1401 XXX This is wrong, but too many callers rely on this behavior. */
1402 if (err != 0)
1403 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001404}
1405
Guido van Rossum82598051997-03-05 00:20:32 +00001406PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001407PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001409{
Neal Norwitze92fba02006-03-04 18:52:26 +00001410 PyObject *ret = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001411 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001412 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001413 if (arena == NULL)
1414 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001415
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001417 if (mod != NULL)
1418 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001419 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001421}
1422
1423PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001424PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001425 PyObject *locals, int closeit, PyCompilerFlags *flags)
1426{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 PyObject *ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001428 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001429 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001430 if (arena == NULL)
1431 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001432
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001433 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001434 flags, NULL, arena);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001435 if (closeit)
1436 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001438 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001440 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001441 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001442 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001444}
1445
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001446static void
1447flush_io(void)
1448{
1449 PyObject *f, *r;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001450 PyObject *type, *value, *traceback;
1451
1452 /* Save the current exception */
1453 PyErr_Fetch(&type, &value, &traceback);
1454
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001455 f = PySys_GetObject("stderr");
1456 if (f != NULL) {
1457 r = PyObject_CallMethod(f, "flush", "");
1458 if (r)
1459 Py_DECREF(r);
1460 else
1461 PyErr_Clear();
1462 }
1463 f = PySys_GetObject("stdout");
1464 if (f != NULL) {
1465 r = PyObject_CallMethod(f, "flush", "");
1466 if (r)
1467 Py_DECREF(r);
1468 else
1469 PyErr_Clear();
1470 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001471
1472 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001473}
1474
Guido van Rossum82598051997-03-05 00:20:32 +00001475static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001478{
Guido van Rossum82598051997-03-05 00:20:32 +00001479 PyCodeObject *co;
1480 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001481 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001482 if (co == NULL)
1483 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001484 v = PyEval_EvalCode(co, globals, locals);
1485 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001486 return v;
1487}
1488
Guido van Rossum82598051997-03-05 00:20:32 +00001489static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001490run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001492{
Guido van Rossum82598051997-03-05 00:20:32 +00001493 PyCodeObject *co;
1494 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001495 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001496 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001497
Guido van Rossum82598051997-03-05 00:20:32 +00001498 magic = PyMarshal_ReadLongFromFile(fp);
1499 if (magic != PyImport_GetMagicNumber()) {
1500 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001501 "Bad magic number in .pyc file");
1502 return NULL;
1503 }
Guido van Rossum82598051997-03-05 00:20:32 +00001504 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001505 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001506 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001507 if (v == NULL || !PyCode_Check(v)) {
1508 Py_XDECREF(v);
1509 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001510 "Bad code object in .pyc file");
1511 return NULL;
1512 }
Guido van Rossum82598051997-03-05 00:20:32 +00001513 co = (PyCodeObject *)v;
1514 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001515 if (v && flags)
1516 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001517 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001518 return v;
1519}
1520
Guido van Rossum82598051997-03-05 00:20:32 +00001521PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001522Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001523 PyCompilerFlags *flags)
1524{
Guido van Rossum82598051997-03-05 00:20:32 +00001525 PyCodeObject *co;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001526 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001527 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001528 if (arena == NULL)
1529 return NULL;
1530
1531 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001532 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001533 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001534 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001535 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001536 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001537 PyObject *result = PyAST_mod2obj(mod);
1538 PyArena_Free(arena);
1539 return result;
1540 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001541 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001542 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001543 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001544}
1545
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001546struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001547Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001548{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001549 struct symtable *st;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001550 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001551 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001552 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001553 if (arena == NULL)
1554 return NULL;
1555
Christian Heimesb1b3efc2008-03-26 23:24:27 +00001556 flags.cf_flags = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001557 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001558 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001559 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001560 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001563 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001564 return st;
1565}
1566
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567/* Preferred access to parser is through AST. */
1568mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001569PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001570 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 mod_ty mod;
1573 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001574 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001575
1576 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001577 &_PyParser_Grammar, start, &err,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001578 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001580 if (flags) {
1581 flags->cf_flags |= iflags & PyCF_MASK;
1582 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001583 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 PyNode_Free(n);
1585 return mod;
1586 }
1587 else {
1588 err_input(&err);
1589 return NULL;
1590 }
1591}
1592
1593mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001594PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1595 int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001596 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001597 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 mod_ty mod;
1600 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001601 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001602
Christian Heimes4d6ec852008-03-26 22:34:47 +00001603 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001604 &_PyParser_Grammar,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001605 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001607 if (flags) {
1608 flags->cf_flags |= iflags & PyCF_MASK;
1609 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001610 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 PyNode_Free(n);
1612 return mod;
1613 }
1614 else {
1615 err_input(&err);
1616 if (errcode)
1617 *errcode = err.error;
1618 return NULL;
1619 }
1620}
1621
Guido van Rossuma110aa61994-08-29 12:50:44 +00001622/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001623
Guido van Rossuma110aa61994-08-29 12:50:44 +00001624node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001625PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001626{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001627 perrdetail err;
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001628 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1629 &_PyParser_Grammar,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001630 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001631 if (n == NULL)
1632 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001633
Guido van Rossuma110aa61994-08-29 12:50:44 +00001634 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001635}
1636
Guido van Rossuma110aa61994-08-29 12:50:44 +00001637/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001638
Guido van Rossuma110aa61994-08-29 12:50:44 +00001639node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001640PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001641{
Tim Petersfe2127d2001-07-16 05:37:24 +00001642 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001643 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1644 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001645 if (n == NULL)
1646 err_input(&err);
1647 return n;
1648}
1649
1650node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001651PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001652 int start, int flags)
1653{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001654 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001655 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1656 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001657 if (n == NULL)
1658 err_input(&err);
1659 return n;
1660}
1661
1662node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001663PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001664{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001666}
1667
Guido van Rossum66ebd912003-04-17 16:02:26 +00001668/* May want to move a more generalized form of this to parsetok.c or
1669 even parser modules. */
1670
1671void
1672PyParser_SetError(perrdetail *err)
1673{
1674 err_input(err);
1675}
1676
Guido van Rossuma110aa61994-08-29 12:50:44 +00001677/* Set the error appropriate to the given input error code (see errcode.h) */
1678
1679static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001680err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001681{
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001682 PyObject *v, *w, *errtype, *errtext;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001683 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001684 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001685 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001686 switch (err->error) {
1687 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001688 errtype = PyExc_IndentationError;
1689 if (err->expected == INDENT)
1690 msg = "expected an indented block";
1691 else if (err->token == INDENT)
1692 msg = "unexpected indent";
1693 else if (err->token == DEDENT)
1694 msg = "unexpected unindent";
1695 else {
1696 errtype = PyExc_SyntaxError;
1697 msg = "invalid syntax";
1698 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001699 break;
1700 case E_TOKEN:
1701 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001702 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001703 case E_EOFS:
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +00001704 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001705 break;
1706 case E_EOLS:
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +00001707 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001708 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001709 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001710 if (!PyErr_Occurred())
1711 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001712 return;
1713 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001714 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001715 return;
1716 case E_EOF:
1717 msg = "unexpected EOF while parsing";
1718 break;
Fred Drake85f36392000-07-11 17:53:00 +00001719 case E_TABSPACE:
1720 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001721 msg = "inconsistent use of tabs and spaces in indentation";
1722 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001723 case E_OVERFLOW:
1724 msg = "expression too long";
1725 break;
Fred Drake85f36392000-07-11 17:53:00 +00001726 case E_DEDENT:
1727 errtype = PyExc_IndentationError;
1728 msg = "unindent does not match any outer indentation level";
1729 break;
1730 case E_TOODEEP:
1731 errtype = PyExc_IndentationError;
1732 msg = "too many levels of indentation";
1733 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001734 case E_DECODE: {
1735 PyObject *type, *value, *tb;
1736 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001737 if (value != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +00001738 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001739 if (u != NULL) {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001740 msg = PyUnicode_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001741 }
1742 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001743 if (msg == NULL)
1744 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001745 Py_XDECREF(type);
1746 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001747 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001748 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001749 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001750 case E_LINECONT:
1751 msg = "unexpected character after line continuation character";
1752 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001753
1754 case E_IDENTIFIER:
1755 msg = "invalid character in identifier";
1756 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001757 default:
1758 fprintf(stderr, "error=%d\n", err->error);
1759 msg = "unknown parsing error";
1760 break;
1761 }
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001762 /* err->text may not be UTF-8 in case of decoding errors.
1763 Explicitly convert to an object. */
1764 if (!err->text) {
1765 errtext = Py_None;
1766 Py_INCREF(Py_None);
1767 } else {
1768 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1769 "replace");
1770 }
1771 v = Py_BuildValue("(ziiN)", err->filename,
1772 err->lineno, err->offset, errtext);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001773 if (err->text != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001774 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 err->text = NULL;
1776 }
1777 w = NULL;
1778 if (v != NULL)
1779 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001780 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001781 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001782 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001783 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001784}
1785
1786/* Print fatal error message and abort */
1787
1788void
Tim Peters7c321a82002-07-09 02:57:01 +00001789Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001790{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001791 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001792 if (PyErr_Occurred()) {
1793 PyErr_Print();
1794 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001795#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001796 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001797 OutputDebugString(msg);
1798 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001799#ifdef _DEBUG
1800 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001801#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001802#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001803 abort();
1804}
1805
1806/* Clean up and exit */
1807
Guido van Rossuma110aa61994-08-29 12:50:44 +00001808#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001809#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001810#endif
1811
Collin Winter670e6922007-03-21 02:57:17 +00001812static void (*pyexitfunc)(void) = NULL;
1813/* For the atexit module. */
1814void _Py_PyAtExit(void (*func)(void))
1815{
1816 pyexitfunc = func;
1817}
1818
1819static void
1820call_py_exitfuncs(void)
1821{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001822 if (pyexitfunc == NULL)
Collin Winter670e6922007-03-21 02:57:17 +00001823 return;
1824
1825 (*pyexitfunc)();
1826 PyErr_Clear();
1827}
1828
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001829#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001830static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001831static int nexitfuncs = 0;
1832
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001833int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001834{
1835 if (nexitfuncs >= NEXITFUNCS)
1836 return -1;
1837 exitfuncs[nexitfuncs++] = func;
1838 return 0;
1839}
1840
Guido van Rossumcc283f51997-08-05 02:22:03 +00001841static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001842call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001843{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001844 while (nexitfuncs > 0)
1845 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001846
1847 fflush(stdout);
1848 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001849}
1850
1851void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001852Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001853{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001854 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001855
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001856 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001857}
1858
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001859static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001860initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001861{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001862#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001863 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001864#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001865#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001866 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001867#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001868#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001869 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001870#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001871 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001872}
1873
Guido van Rossum7433b121997-02-14 19:45:36 +00001874
1875/*
1876 * The file descriptor fd is considered ``interactive'' if either
1877 * a) isatty(fd) is TRUE, or
1878 * b) the -i flag was given, and the filename associated with
1879 * the descriptor is NULL or "<stdin>" or "???".
1880 */
1881int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001882Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001883{
1884 if (isatty((int)fileno(fp)))
1885 return 1;
1886 if (!Py_InteractiveFlag)
1887 return 0;
1888 return (filename == NULL) ||
1889 (strcmp(filename, "<stdin>") == 0) ||
1890 (strcmp(filename, "???") == 0);
1891}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001892
1893
Tim Petersd08e3822003-04-17 15:24:21 +00001894#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001895#if defined(WIN32) && defined(_MSC_VER)
1896
1897/* Stack checking for Microsoft C */
1898
1899#include <malloc.h>
1900#include <excpt.h>
1901
Fred Drakee8de31c2000-08-31 05:38:39 +00001902/*
1903 * Return non-zero when we run out of memory on the stack; zero otherwise.
1904 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001905int
Fred Drake399739f2000-08-31 05:52:44 +00001906PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001907{
1908 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001909 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001910 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001911 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001912 return 0;
Christian Heimes7131fd92008-02-19 14:21:46 +00001913 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
Trent Nelson39e307e2008-03-19 06:45:48 +00001914 EXCEPTION_EXECUTE_HANDLER :
1915 EXCEPTION_CONTINUE_SEARCH) {
Christian Heimes7131fd92008-02-19 14:21:46 +00001916 int errcode = _resetstkoflw();
1917 if (errcode)
1918 {
1919 Py_FatalError("Could not reset the stack!");
1920 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001921 }
1922 return 1;
1923}
1924
1925#endif /* WIN32 && _MSC_VER */
1926
1927/* Alternate implementations can be added here... */
1928
1929#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001930
1931
1932/* Wrappers around sigaction() or signal(). */
1933
1934PyOS_sighandler_t
1935PyOS_getsig(int sig)
1936{
1937#ifdef HAVE_SIGACTION
1938 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001939 if (sigaction(sig, NULL, &context) == -1)
1940 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001941 return context.sa_handler;
1942#else
1943 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001944/* Special signal handling for the secure CRT in Visual Studio 2005 */
1945#if defined(_MSC_VER) && _MSC_VER >= 1400
1946 switch (sig) {
1947 /* Only these signals are valid */
1948 case SIGINT:
1949 case SIGILL:
1950 case SIGFPE:
1951 case SIGSEGV:
1952 case SIGTERM:
1953 case SIGBREAK:
1954 case SIGABRT:
1955 break;
1956 /* Don't call signal() with other values or it will assert */
1957 default:
1958 return SIG_ERR;
1959 }
1960#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001961 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001962 if (handler != SIG_ERR)
1963 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001964 return handler;
1965#endif
1966}
1967
1968PyOS_sighandler_t
1969PyOS_setsig(int sig, PyOS_sighandler_t handler)
1970{
1971#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001972 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001973 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001974 sigemptyset(&context.sa_mask);
1975 context.sa_flags = 0;
1976 if (sigaction(sig, &context, &ocontext) == -1)
1977 return SIG_ERR;
1978 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001979#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001980 PyOS_sighandler_t oldhandler;
1981 oldhandler = signal(sig, handler);
1982#ifdef HAVE_SIGINTERRUPT
1983 siginterrupt(sig, 1);
1984#endif
1985 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001986#endif
1987}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988
1989/* Deprecated C API functions still provided for binary compatiblity */
1990
1991#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001992PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1994{
1995 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1996}
1997
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001998#undef PyParser_SimpleParseString
1999PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000PyParser_SimpleParseString(const char *str, int start)
2001{
2002 return PyParser_SimpleParseStringFlags(str, start, 0);
2003}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002004
2005#undef PyRun_AnyFile
2006PyAPI_FUNC(int)
2007PyRun_AnyFile(FILE *fp, const char *name)
2008{
2009 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
2010}
2011
2012#undef PyRun_AnyFileEx
2013PyAPI_FUNC(int)
2014PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2015{
2016 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
2017}
2018
2019#undef PyRun_AnyFileFlags
2020PyAPI_FUNC(int)
2021PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2022{
2023 return PyRun_AnyFileExFlags(fp, name, 0, flags);
2024}
2025
2026#undef PyRun_File
2027PyAPI_FUNC(PyObject *)
2028PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2029{
2030 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2031}
2032
2033#undef PyRun_FileEx
2034PyAPI_FUNC(PyObject *)
2035PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2036{
2037 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2038}
2039
2040#undef PyRun_FileFlags
2041PyAPI_FUNC(PyObject *)
2042PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2043 PyCompilerFlags *flags)
2044{
2045 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2046}
2047
2048#undef PyRun_SimpleFile
2049PyAPI_FUNC(int)
2050PyRun_SimpleFile(FILE *f, const char *p)
2051{
2052 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2053}
2054
2055#undef PyRun_SimpleFileEx
2056PyAPI_FUNC(int)
2057PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2058{
2059 return PyRun_SimpleFileExFlags(f, p, c, NULL);
2060}
2061
2062
2063#undef PyRun_String
2064PyAPI_FUNC(PyObject *)
2065PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2066{
2067 return PyRun_StringFlags(str, s, g, l, NULL);
2068}
2069
2070#undef PyRun_SimpleString
2071PyAPI_FUNC(int)
2072PyRun_SimpleString(const char *s)
2073{
2074 return PyRun_SimpleStringFlags(s, NULL);
2075}
2076
2077#undef Py_CompileString
2078PyAPI_FUNC(PyObject *)
2079Py_CompileString(const char *str, const char *p, int s)
2080{
2081 return Py_CompileStringFlags(str, p, s, NULL);
2082}
2083
2084#undef PyRun_InteractiveOne
2085PyAPI_FUNC(int)
2086PyRun_InteractiveOne(FILE *f, const char *p)
2087{
2088 return PyRun_InteractiveOneFlags(f, p, NULL);
2089}
2090
2091#undef PyRun_InteractiveLoop
2092PyAPI_FUNC(int)
2093PyRun_InteractiveLoop(FILE *f, const char *p)
2094{
2095 return PyRun_InteractiveLoopFlags(f, p, NULL);
2096}
2097
2098#ifdef __cplusplus
2099}
2100#endif