blob: a80cb143fadd91304403f1cff749b690d33c77c2 [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 */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000086
Christian Heimes33fe8092008-04-13 13:53:33 +000087/* PyModule_GetWarningsModule is no longer necessary as of 2.6
88since _warnings is builtin. This API should not be used. */
89PyObject *
90PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000091{
Christian Heimes33fe8092008-04-13 13:53:33 +000092 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000093}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000094
Guido van Rossum25ce5661997-08-02 03:10:38 +000095static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096
Thomas Wouters7e474022000-07-16 12:04:32 +000097/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000098
99int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000101{
102 return initialized;
103}
104
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105/* Global initializations. Can be undone by Py_Finalize(). Don't
106 call this twice without an intervening Py_Finalize() call. When
107 initializations fail, a fatal error is issued and the function does
108 not return. On return, the first thread and interpreter state have
109 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 Locking: you must hold the interpreter lock while calling this.
112 (If the lock has not yet been initialized, that's equivalent to
113 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000117static int
118add_flag(int flag, const char *envs)
119{
120 int env = atoi(envs);
121 if (flag < env)
122 flag = env;
123 if (flag < 1)
124 flag = 1;
125 return flag;
126}
127
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000129Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 PyThreadState *tstate;
Guido van Rossum826d8972007-10-30 18:34:07 +0000133 PyObject *bimod, *sysmod, *pstderr;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000134 char *p;
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000135#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000136 char *codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000137#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000138 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000139
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000140 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000141 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000142 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000143
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000144#ifdef HAVE_SETLOCALE
145 /* Set up the LC_CTYPE locale, so we can obtain
146 the locale's charset without having to switch
147 locales. */
148 setlocale(LC_CTYPE, "");
149#endif
150
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000151 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000152 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000153 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000154 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000155 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000156 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Christian Heimes790c8232008-01-07 21:14:23 +0000157 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
158 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000159
Guido van Rossuma027efa1997-05-05 20:56:21 +0000160 interp = PyInterpreterState_New();
161 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000162 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000163
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 tstate = PyThreadState_New(interp);
165 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000167 (void) PyThreadState_Swap(tstate);
168
Guido van Rossum70d893a2001-08-16 08:21:42 +0000169 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000170
Neal Norwitzb2501f42002-12-31 03:42:13 +0000171 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000172 Py_FatalError("Py_Initialize: can't init frames");
173
Guido van Rossumddefaf32007-01-14 03:31:43 +0000174 if (!_PyLong_Init())
175 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000176
Neal Norwitz6968b052007-02-27 19:02:19 +0000177 if (!PyBytes_Init())
178 Py_FatalError("Py_Initialize: can't init bytes");
179
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000180 _PyFloat_Init();
181
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182 interp->modules = PyDict_New();
183 if (interp->modules == NULL)
184 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000185 interp->modules_reloading = PyDict_New();
186 if (interp->modules_reloading == NULL)
187 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000188
Guido van Rossumc94044c2000-03-10 23:03:54 +0000189 /* Init Unicode implementation; relies on the codec registry */
190 _PyUnicode_Init();
191
Barry Warsawf242aa02000-05-25 23:09:49 +0000192 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193 if (bimod == NULL)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000194 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000195 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000196 if (interp->builtins == NULL)
197 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000198 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000200 /* initialize builtin exceptions */
201 _PyExc_Init();
202
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203 sysmod = _PySys_Init();
204 if (sysmod == NULL)
205 Py_FatalError("Py_Initialize: can't initialize sys");
206 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000207 if (interp->sysdict == NULL)
208 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209 Py_INCREF(interp->sysdict);
210 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000211 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 PyDict_SetItemString(interp->sysdict, "modules",
213 interp->modules);
214
Guido van Rossum826d8972007-10-30 18:34:07 +0000215 /* Set up a preliminary stderr printer until we have enough
216 infrastructure for the io module in place. */
217 pstderr = PyFile_NewStdPrinter(fileno(stderr));
218 if (pstderr == NULL)
219 Py_FatalError("Py_Initialize: can't set preliminary stderr");
220 PySys_SetObject("stderr", pstderr);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000221 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000222
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000223 _PyImport_Init();
224
Barry Warsaw035574d1997-08-29 22:07:17 +0000225 /* phase 2 of builtins */
Georg Brandl1a3284e2007-12-02 09:40:06 +0000226 _PyImport_FixupExtension("builtins", "builtins");
Barry Warsaw035574d1997-08-29 22:07:17 +0000227
Just van Rossum52e14d62002-12-30 22:08:05 +0000228 _PyImportHooks_Init();
229
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000230 if (install_sigs)
231 initsigs(); /* Signal handling stuff, including initintr() */
Christian Heimes33fe8092008-04-13 13:53:33 +0000232
233 /* Initialize warnings. */
234 _PyWarnings_Init();
235 if (PySys_HasWarnOptions()) {
236 PyObject *warnings_module = PyImport_ImportModule("warnings");
237 if (!warnings_module)
238 PyErr_Clear();
239 Py_XDECREF(warnings_module);
240 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000241
242 initmain(); /* Module __main__ */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000243 if (initstdio() < 0)
244 Py_FatalError(
245 "Py_Initialize: can't initialize sys standard streams");
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000246 if (!Py_NoSiteFlag)
247 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000248
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249 /* auto-thread-state API, if available */
250#ifdef WITH_THREAD
251 _PyGILState_Init(interp, tstate);
252#endif /* WITH_THREAD */
253
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000254#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000255 /* On Unix, set the file system encoding according to the
256 user's preference, if the CODESET names a well-known
257 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000258 initialized by other means. Also set the encoding of
259 stdin and stdout if these are terminals. */
260
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000261 codeset = nl_langinfo(CODESET);
262 if (codeset && *codeset) {
263 PyObject *enc = PyCodec_Encoder(codeset);
264 if (enc) {
265 codeset = strdup(codeset);
266 Py_DECREF(enc);
267 } else {
268 codeset = NULL;
269 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000270 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271 } else
272 codeset = NULL;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000273
274 if (codeset) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000275 if (!Py_FileSystemDefaultEncoding)
276 Py_FileSystemDefaultEncoding = codeset;
277 else
278 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000279 }
280#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000281}
282
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000283void
284Py_Initialize(void)
285{
286 Py_InitializeEx(1);
287}
288
289
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000290#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000292#endif
293
Guido van Rossume8432ac2007-07-09 15:04:50 +0000294/* Flush stdout and stderr */
295
Neal Norwitz2bad9702007-08-27 06:19:22 +0000296static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000297flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000298{
299 PyObject *fout = PySys_GetObject("stdout");
300 PyObject *ferr = PySys_GetObject("stderr");
301 PyObject *tmp;
302
Christian Heimes2be03732007-11-15 02:26:46 +0000303 if (fout != NULL && fout != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000304 tmp = PyObject_CallMethod(fout, "flush", "");
305 if (tmp == NULL)
306 PyErr_Clear();
307 else
308 Py_DECREF(tmp);
309 }
310
Christian Heimes2be03732007-11-15 02:26:46 +0000311 if (ferr != NULL || ferr != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000312 tmp = PyObject_CallMethod(ferr, "flush", "");
313 if (tmp == NULL)
314 PyErr_Clear();
315 else
316 Py_DECREF(tmp);
317 }
318}
319
Guido van Rossum25ce5661997-08-02 03:10:38 +0000320/* Undo the effect of Py_Initialize().
321
322 Beware: if multiple interpreter and/or thread states exist, these
323 are not wiped out; only the current thread and interpreter state
324 are deleted. But since everything else is deleted, those other
325 interpreter and thread states should no longer be used.
326
327 (XXX We should do better, e.g. wipe out all interpreters and
328 threads.)
329
330 Locking: as above.
331
332*/
333
334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336{
337 PyInterpreterState *interp;
338 PyThreadState *tstate;
339
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000340 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000341 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342
Tim Peters384fd102001-01-21 03:40:37 +0000343 /* The interpreter is still entirely intact at this point, and the
344 * exit funcs may be relying on that. In particular, if some thread
345 * or exit func is still waiting to do an import, the import machinery
346 * expects Py_IsInitialized() to return true. So don't say the
347 * interpreter is uninitialized until after the exit funcs have run.
348 * Note that Threading.py uses an exit func to do a join on all the
349 * threads created thru it, so this also protects pending imports in
350 * the threads created via Threading.
351 */
Collin Winter670e6922007-03-21 02:57:17 +0000352 call_py_exitfuncs();
Tim Peters384fd102001-01-21 03:40:37 +0000353 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000354
Guido van Rossume8432ac2007-07-09 15:04:50 +0000355 /* Flush stdout+stderr */
356 flush_std_files();
357
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000358 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000359 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000360 interp = tstate->interp;
361
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000362 /* Disable signal handling */
363 PyOS_FiniInterrupts();
364
Christian Heimes26855632008-01-27 23:50:43 +0000365 /* Clear type lookup cache */
366 PyType_ClearCache();
367
Guido van Rossume13ddc92003-04-17 17:29:22 +0000368 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000369 * before all modules are destroyed.
370 * XXX If a __del__ or weakref callback is triggered here, and tries to
371 * XXX import a module, bad things can happen, because Python no
372 * XXX longer believes it's initialized.
373 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
374 * XXX is easy to provoke that way. I've also seen, e.g.,
375 * XXX Exception exceptions.ImportError: 'No module named sha'
376 * XXX in <function callback at 0x008F5718> ignored
377 * XXX but I'm unclear on exactly how that one happens. In any case,
378 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000379 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000380 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381#ifdef COUNT_ALLOCS
382 /* With COUNT_ALLOCS, it helps to run GC multiple times:
383 each collection might release some types from the type
384 list, so they become garbage. */
385 while (PyGC_Collect() > 0)
386 /* nothing */;
387#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000388
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000389 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000390 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000391
Guido van Rossume8432ac2007-07-09 15:04:50 +0000392 /* Flush stdout+stderr (again, in case more was printed) */
393 flush_std_files();
394
Guido van Rossume13ddc92003-04-17 17:29:22 +0000395 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000396 * new-style class definitions, for example.
397 * XXX This is disabled because it caused too many problems. If
398 * XXX a __del__ or weakref callback triggers here, Python code has
399 * XXX a hard time running, because even the sys module has been
400 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
401 * XXX One symptom is a sequence of information-free messages
402 * XXX coming from threads (if a __del__ or callback is invoked,
403 * XXX other threads can execute too, and any exception they encounter
404 * XXX triggers a comedy of errors as subsystem after subsystem
405 * XXX fails to find what it *expects* to find in sys to help report
406 * XXX the exception and consequent unexpected failures). I've also
407 * XXX seen segfaults then, after adding print statements to the
408 * XXX Python code getting called.
409 */
410#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000411 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000412#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000413
Guido van Rossum1707aad1997-12-08 23:43:45 +0000414 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
415 _PyImport_Fini();
416
417 /* Debugging stuff */
418#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000420#endif
421
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000423
Tim Peters9cf25ce2003-04-17 15:21:01 +0000424#ifdef Py_TRACE_REFS
425 /* Display all objects still alive -- this can invoke arbitrary
426 * __repr__ overrides, so requires a mostly-intact interpreter.
427 * Alas, a lot of stuff may still be alive now that will be cleaned
428 * up later.
429 */
Tim Peters269b2a62003-04-17 19:52:29 +0000430 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000431 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000432#endif /* Py_TRACE_REFS */
433
Guido van Rossumd922fa42003-04-15 14:10:09 +0000434 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000435 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000436
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000437 /* Now we decref the exception classes. After this point nothing
438 can raise an exception. That's okay, because each Fini() method
439 below has been checked to make sure no exceptions are ever
440 raised.
441 */
442
443 _PyExc_Fini();
444
Christian Heimes7d2ff882007-11-30 14:35:04 +0000445 /* Cleanup auto-thread-state */
446#ifdef WITH_THREAD
447 _PyGILState_Fini();
448#endif /* WITH_THREAD */
449
Guido van Rossumd922fa42003-04-15 14:10:09 +0000450 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000451 PyThreadState_Swap(NULL);
452 PyInterpreterState_Delete(interp);
453
Guido van Rossumd922fa42003-04-15 14:10:09 +0000454 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000455 PyMethod_Fini();
456 PyFrame_Fini();
457 PyCFunction_Fini();
458 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000459 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000460 PySet_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000461 PyString_Fini();
Neal Norwitz6968b052007-02-27 19:02:19 +0000462 PyBytes_Fini();
Guido van Rossumddefaf32007-01-14 03:31:43 +0000463 PyLong_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000464 PyFloat_Fini();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000465 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000466
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000467 /* Cleanup Unicode implementation */
468 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000469
Christian Heimesc8967002007-11-30 10:18:26 +0000470 /* reset file system default encoding */
471 if (!Py_HasFileSystemDefaultEncoding) {
472 free((char*)Py_FileSystemDefaultEncoding);
Trent Nelson39e307e2008-03-19 06:45:48 +0000473 Py_FileSystemDefaultEncoding = NULL;
Christian Heimesc8967002007-11-30 10:18:26 +0000474 }
475
Guido van Rossumcc283f51997-08-05 02:22:03 +0000476 /* XXX Still allocated:
477 - various static ad-hoc pointers to interned strings
478 - int and float free list blocks
479 - whatever various modules and libraries allocate
480 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000481
482 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000483
Tim Peters269b2a62003-04-17 19:52:29 +0000484#ifdef Py_TRACE_REFS
485 /* Display addresses (& refcnts) of all objects still alive.
486 * An address can be used to find the repr of the object, printed
487 * above by _Py_PrintReferences.
488 */
489 if (Py_GETENV("PYTHONDUMPREFS"))
490 _Py_PrintReferenceAddresses(stderr);
491#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000492#ifdef PYMALLOC_DEBUG
493 if (Py_GETENV("PYTHONMALLOCSTATS"))
494 _PyObject_DebugMallocStats();
495#endif
496
Guido van Rossumcc283f51997-08-05 02:22:03 +0000497 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000498}
499
500/* Create and initialize a new interpreter and thread, and return the
501 new thread. This requires that Py_Initialize() has been called
502 first.
503
504 Unsuccessful initialization yields a NULL pointer. Note that *no*
505 exception information is available even in this case -- the
506 exception information is held in the thread, and there is no
507 thread.
508
509 Locking: as above.
510
511*/
512
513PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000514Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000515{
516 PyInterpreterState *interp;
517 PyThreadState *tstate, *save_tstate;
518 PyObject *bimod, *sysmod;
519
520 if (!initialized)
521 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
522
523 interp = PyInterpreterState_New();
524 if (interp == NULL)
525 return NULL;
526
527 tstate = PyThreadState_New(interp);
528 if (tstate == NULL) {
529 PyInterpreterState_Delete(interp);
530 return NULL;
531 }
532
533 save_tstate = PyThreadState_Swap(tstate);
534
535 /* XXX The following is lax in error checking */
536
537 interp->modules = PyDict_New();
Guido van Rossumd8faa362007-04-27 19:54:29 +0000538 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000539
Georg Brandl1a3284e2007-12-02 09:40:06 +0000540 bimod = _PyImport_FindExtension("builtins", "builtins");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000541 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000542 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000543 if (interp->builtins == NULL)
544 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000545 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000546 }
547 sysmod = _PyImport_FindExtension("sys", "sys");
548 if (bimod != NULL && sysmod != NULL) {
549 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000550 if (interp->sysdict == NULL)
551 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552 Py_INCREF(interp->sysdict);
553 PySys_SetPath(Py_GetPath());
554 PyDict_SetItemString(interp->sysdict, "modules",
555 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000556 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000558 if (!Py_NoSiteFlag)
559 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000560 }
561
562 if (!PyErr_Occurred())
563 return tstate;
564
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000566 /* Oops, it didn't work. Undo it all. */
567
568 PyErr_Print();
569 PyThreadState_Clear(tstate);
570 PyThreadState_Swap(save_tstate);
571 PyThreadState_Delete(tstate);
572 PyInterpreterState_Delete(interp);
573
574 return NULL;
575}
576
577/* Delete an interpreter and its last thread. This requires that the
578 given thread state is current, that the thread has no remaining
579 frames, and that it is its interpreter's only remaining thread.
580 It is a fatal error to violate these constraints.
581
582 (Py_Finalize() doesn't have these constraints -- it zaps
583 everything, regardless.)
584
585 Locking: as above.
586
587*/
588
589void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000590Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000591{
592 PyInterpreterState *interp = tstate->interp;
593
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000594 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595 Py_FatalError("Py_EndInterpreter: thread is not current");
596 if (tstate->frame != NULL)
597 Py_FatalError("Py_EndInterpreter: thread still has a frame");
598 if (tstate != interp->tstate_head || tstate->next != NULL)
599 Py_FatalError("Py_EndInterpreter: not the last thread");
600
601 PyImport_Cleanup();
602 PyInterpreterState_Clear(interp);
603 PyThreadState_Swap(NULL);
604 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000605}
606
Martin v. Löwis790465f2008-04-05 20:41:37 +0000607static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000608
609void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000610Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000611{
612 if (pn && *pn)
613 progname = pn;
614}
615
Martin v. Löwis790465f2008-04-05 20:41:37 +0000616wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000617Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000618{
619 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000620}
621
Martin v. Löwis790465f2008-04-05 20:41:37 +0000622static wchar_t *default_home = NULL;
623static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000624
625void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000626Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000627{
628 default_home = home;
629}
630
Martin v. Löwis790465f2008-04-05 20:41:37 +0000631wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000632Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000633{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000634 wchar_t *home = default_home;
635 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
636 char* chome = Py_GETENV("PYTHONHOME");
637 if (chome) {
638 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
639 if (r != (size_t)-1 && r <= PATH_MAX)
640 home = env_home;
641 }
642
643 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000644 return home;
645}
646
Guido van Rossum6135a871995-01-09 17:53:26 +0000647/* Create __main__ module */
648
649static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000651{
Guido van Rossum82598051997-03-05 00:20:32 +0000652 PyObject *m, *d;
653 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000654 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000655 Py_FatalError("can't create __main__ module");
656 d = PyModule_GetDict(m);
657 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Georg Brandl1a3284e2007-12-02 09:40:06 +0000658 PyObject *bimod = PyImport_ImportModule("builtins");
Guido van Rossum858cb731997-11-19 16:15:37 +0000659 if (bimod == NULL ||
660 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000661 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000662 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000663 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000664}
665
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000666/* Import the site module (not into __main__ though) */
667
668static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000669initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000670{
671 PyObject *m, *f;
672 m = PyImport_ImportModule("site");
673 if (m == NULL) {
674 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000675 if (f == NULL || f == Py_None)
676 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000677 if (Py_VerboseFlag) {
678 PyFile_WriteString(
679 "'import site' failed; traceback:\n", f);
680 PyErr_Print();
681 }
682 else {
683 PyFile_WriteString(
684 "'import site' failed; use -v for traceback\n", f);
685 PyErr_Clear();
686 }
687 }
688 else {
689 Py_DECREF(m);
690 }
691}
692
Georg Brandl1a3284e2007-12-02 09:40:06 +0000693/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000694static int
695initstdio(void)
696{
697 PyObject *iomod = NULL, *wrapper;
698 PyObject *bimod = NULL;
699 PyObject *m;
700 PyObject *std = NULL;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000701 int status = 0, fd;
Trent Nelson39e307e2008-03-19 06:45:48 +0000702 PyObject * encoding_attr;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000703
704 /* Hack to avoid a nasty recursion issue when Python is invoked
705 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
706 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
707 goto error;
708 }
709 Py_DECREF(m);
710
711 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
712 goto error;
713 }
714 Py_DECREF(m);
715
Georg Brandl1a3284e2007-12-02 09:40:06 +0000716 if (!(bimod = PyImport_ImportModule("builtins"))) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000717 goto error;
718 }
719
720 if (!(iomod = PyImport_ImportModule("io"))) {
721 goto error;
722 }
723 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
724 goto error;
725 }
726
Georg Brandl1a3284e2007-12-02 09:40:06 +0000727 /* Set builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000728 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
729 goto error;
730 }
731
732 /* Set sys.stdin */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000733 fd = fileno(stdin);
734 /* Under some conditions stdin, stdout and stderr may not be connected
735 * and fileno() may point to an invalid file descriptor. For example
736 * GUI apps don't have valid standard streams by default.
737 */
738 if (fd < 0) {
739#ifdef MS_WINDOWS
740 std = Py_None;
741 Py_INCREF(std);
742#else
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000743 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000744#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000745 }
Christian Heimes58cb1b82007-11-13 02:19:40 +0000746 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000747 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000748 "\n", 0))) {
749 goto error;
750 }
751 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000752 PySys_SetObject("__stdin__", std);
753 PySys_SetObject("stdin", std);
754 Py_DECREF(std);
755
756 /* Set sys.stdout */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000757 fd = fileno(stdout);
758 if (fd < 0) {
759#ifdef MS_WINDOWS
760 std = Py_None;
761 Py_INCREF(std);
762#else
763 goto error;
764#endif
765 }
766 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000767 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000768 "\n", 0))) {
769 goto error;
770 }
771 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000772 PySys_SetObject("__stdout__", std);
773 PySys_SetObject("stdout", std);
774 Py_DECREF(std);
775
Guido van Rossum98297ee2007-11-06 21:34:58 +0000776#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000777 /* Set sys.stderr, replaces the preliminary stderr */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000778 fd = fileno(stderr);
779 if (fd < 0) {
780#ifdef MS_WINDOWS
781 std = Py_None;
782 Py_INCREF(std);
783#else
784 goto error;
785#endif
786 }
787 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000788 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000789 "\n", 0))) {
790 goto error;
791 }
792 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +0000793
794 /* Same as hack above, pre-import stderr's codec to avoid recursion
795 when import.c tries to write to stderr in verbose mode. */
796 encoding_attr = PyObject_GetAttrString(std, "encoding");
797 if (encoding_attr != NULL) {
798 const char * encoding;
799 encoding = PyUnicode_AsString(encoding_attr);
800 if (encoding != NULL) {
801 _PyCodec_Lookup(encoding);
802 }
803 }
804 PyErr_Clear(); /* Not a fatal error if codec isn't available */
805
Christian Heimesdb233082007-11-13 02:34:21 +0000806 PySys_SetObject("__stderr__", std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000807 PySys_SetObject("stderr", std);
808 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000809#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000810
Christian Heimes58cb1b82007-11-13 02:19:40 +0000811 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000812 error:
Christian Heimesdb233082007-11-13 02:34:21 +0000813 status = -1;
814 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000815
816 Py_XDECREF(bimod);
817 Py_XDECREF(iomod);
818 return status;
819}
820
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000821/* Parse input from a file and execute it */
822
823int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000824PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000825 PyCompilerFlags *flags)
826{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000827 if (filename == NULL)
828 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000829 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000830 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000831 if (closeit)
832 fclose(fp);
833 return err;
834 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000835 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000836 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000837}
838
839int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000840PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000841{
Guido van Rossum82598051997-03-05 00:20:32 +0000842 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000844 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000845
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000846 if (flags == NULL) {
847 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000848 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000849 }
Guido van Rossum82598051997-03-05 00:20:32 +0000850 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000852 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000853 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000854 }
Guido van Rossum82598051997-03-05 00:20:32 +0000855 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000856 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000857 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000858 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000859 }
860 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000861 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000862 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000863 if (ret == E_EOF)
864 return 0;
865 /*
866 if (ret == E_NOMEM)
867 return -1;
868 */
869 }
870}
871
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000872/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000873#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000874 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Guido van Rossum45aecf42006-03-15 04:58:47 +0000875 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000876
Thomas Wouters89f507f2006-12-13 04:49:30 +0000877#if 0
878/* Keep an example of flags with future keyword support. */
879#define PARSER_FLAGS(flags) \
880 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
881 PyPARSE_DONT_IMPLY_DEDENT : 0) \
882 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
883 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
884#endif
885
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000886int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000887PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000888{
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000889 PyObject *m, *d, *v, *w, *oenc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000891 PyArena *arena;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000892 char *ps1 = "", *ps2 = "", *enc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000894
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000895 if (fp == stdin) {
896 /* Fetch encoding from sys.stdin */
897 v = PySys_GetObject("stdin");
Christian Heimes2be03732007-11-15 02:26:46 +0000898 if (v == NULL || v == Py_None)
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000899 return -1;
900 oenc = PyObject_GetAttrString(v, "encoding");
901 if (!oenc)
902 return -1;
903 enc = PyUnicode_AsString(oenc);
904 }
Guido van Rossum82598051997-03-05 00:20:32 +0000905 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000906 if (v != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000907 v = PyObject_Str(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000908 if (v == NULL)
909 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000910 else if (PyUnicode_Check(v))
911 ps1 = PyUnicode_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000912 }
Guido van Rossum82598051997-03-05 00:20:32 +0000913 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000914 if (w != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000915 w = PyObject_Str(w);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000916 if (w == NULL)
917 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000918 else if (PyUnicode_Check(w))
919 ps2 = PyUnicode_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000920 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000921 arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922 if (arena == NULL) {
923 Py_XDECREF(v);
924 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000925 Py_XDECREF(oenc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000926 return -1;
927 }
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000928 mod = PyParser_ASTFromFile(fp, filename, enc,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000930 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000931 Py_XDECREF(v);
932 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000933 Py_XDECREF(oenc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000935 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 if (errcode == E_EOF) {
937 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000938 return E_EOF;
939 }
Guido van Rossum82598051997-03-05 00:20:32 +0000940 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000942 }
Guido van Rossum82598051997-03-05 00:20:32 +0000943 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000944 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000945 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000946 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000947 }
Guido van Rossum82598051997-03-05 00:20:32 +0000948 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000949 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000950 PyArena_Free(arena);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +0000951 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000952 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000953 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954 return -1;
955 }
Guido van Rossum82598051997-03-05 00:20:32 +0000956 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957 return 0;
958}
959
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000960/* Check whether a file maybe a pyc file: Look at the extension,
961 the file type, and, if we may close it, at the first few bytes. */
962
963static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000964maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000965{
966 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
967 return 1;
968
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000969 /* Only look into the file if we are allowed to close it, since
970 it then should also be seekable. */
971 if (closeit) {
972 /* Read only two bytes of the magic. If the file was opened in
973 text mode, the bytes 3 and 4 of the magic (\r\n) might not
974 be read as they are on disk. */
975 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
976 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000977 /* Mess: In case of -x, the stream is NOT at its start now,
978 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000979 which makes the current stream position formally undefined,
980 and a x-platform nightmare.
981 Unfortunately, we have no direct way to know whether -x
982 was specified. So we use a terrible hack: if the current
983 stream position is not 0, we assume -x was specified, and
984 give up. Bug 132850 on SourceForge spells out the
985 hopelessness of trying anything else (fseek and ftell
986 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000987 */
Tim Peters3e876562001-02-11 04:35:39 +0000988 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000989 if (ftell(fp) == 0) {
990 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000991 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000992 ispyc = 1;
993 rewind(fp);
994 }
Tim Peters3e876562001-02-11 04:35:39 +0000995 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000996 }
997 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000998}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000999
Guido van Rossum0df002c2000-08-27 19:21:52 +00001000int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001001PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001002 PyCompilerFlags *flags)
1003{
Guido van Rossum82598051997-03-05 00:20:32 +00001004 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001005 const char *ext;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001006 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001007
Guido van Rossum82598051997-03-05 00:20:32 +00001008 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001009 if (m == NULL)
1010 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001011 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +00001012 if (PyDict_GetItemString(d, "__file__") == NULL) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001013 PyObject *f;
1014 f = PyUnicode_DecodeFSDefault(filename);
Fred Drake8ed02042002-10-17 21:24:58 +00001015 if (f == NULL)
1016 return -1;
1017 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1018 Py_DECREF(f);
1019 return -1;
1020 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001021 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +00001022 Py_DECREF(f);
1023 }
Guido van Rossumfdef2711994-09-14 13:31:04 +00001024 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001025 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001026 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +00001027 if (closeit)
1028 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +00001029 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001030 fprintf(stderr, "python: Can't reopen .pyc file\n");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001031 ret = -1;
1032 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001033 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +00001034 /* Turn on optimization if a .pyo file is given */
1035 if (strcmp(ext, ".pyo") == 0)
1036 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001037 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001038 } else {
Tim Petersd08e3822003-04-17 15:24:21 +00001039 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001040 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001041 }
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +00001042 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001043 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001044 PyErr_Print();
Guido van Rossumd8faa362007-04-27 19:54:29 +00001045 ret = -1;
1046 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001047 }
Guido van Rossum82598051997-03-05 00:20:32 +00001048 Py_DECREF(v);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049 ret = 0;
1050 done:
1051 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1052 PyErr_Clear();
1053 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001054}
1055
1056int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001057PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001058{
Guido van Rossum82598051997-03-05 00:20:32 +00001059 PyObject *m, *d, *v;
1060 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001061 if (m == NULL)
1062 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001063 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +00001064 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001066 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001067 return -1;
1068 }
Guido van Rossum82598051997-03-05 00:20:32 +00001069 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001070 return 0;
1071}
1072
Barry Warsaw035574d1997-08-29 22:07:17 +00001073static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001074parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1075 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001076{
1077 long hold;
1078 PyObject *v;
1079
1080 /* old style errors */
1081 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +00001082 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001083 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001084
1085 /* new style errors. `err' is an instance */
1086
1087 if (! (v = PyObject_GetAttrString(err, "msg")))
1088 goto finally;
1089 *message = v;
1090
1091 if (!(v = PyObject_GetAttrString(err, "filename")))
1092 goto finally;
1093 if (v == Py_None)
1094 *filename = NULL;
Kurt B. Kaiser43b15092008-01-05 04:32:22 +00001095 else if (! (*filename = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001096 goto finally;
1097
1098 Py_DECREF(v);
1099 if (!(v = PyObject_GetAttrString(err, "lineno")))
1100 goto finally;
Christian Heimes217cfd12007-12-02 14:31:20 +00001101 hold = PyLong_AsLong(v);
Barry Warsaw035574d1997-08-29 22:07:17 +00001102 Py_DECREF(v);
1103 v = NULL;
1104 if (hold < 0 && PyErr_Occurred())
1105 goto finally;
1106 *lineno = (int)hold;
1107
1108 if (!(v = PyObject_GetAttrString(err, "offset")))
1109 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001110 if (v == Py_None) {
1111 *offset = -1;
1112 Py_DECREF(v);
1113 v = NULL;
1114 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001115 hold = PyLong_AsLong(v);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001116 Py_DECREF(v);
1117 v = NULL;
1118 if (hold < 0 && PyErr_Occurred())
1119 goto finally;
1120 *offset = (int)hold;
1121 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001122
1123 if (!(v = PyObject_GetAttrString(err, "text")))
1124 goto finally;
1125 if (v == Py_None)
1126 *text = NULL;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001127 else if (!PyUnicode_Check(v) ||
1128 !(*text = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001129 goto finally;
1130 Py_DECREF(v);
1131 return 1;
1132
1133finally:
1134 Py_XDECREF(v);
1135 return 0;
1136}
1137
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001138void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001139PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001140{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001141 PyErr_PrintEx(1);
1142}
1143
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001144static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001145print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001146{
1147 char *nl;
1148 if (offset >= 0) {
1149 if (offset > 0 && offset == (int)strlen(text))
1150 offset--;
1151 for (;;) {
1152 nl = strchr(text, '\n');
1153 if (nl == NULL || nl-text >= offset)
1154 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001155 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001156 text = nl+1;
1157 }
1158 while (*text == ' ' || *text == '\t') {
1159 text++;
1160 offset--;
1161 }
1162 }
1163 PyFile_WriteString(" ", f);
1164 PyFile_WriteString(text, f);
1165 if (*text == '\0' || text[strlen(text)-1] != '\n')
1166 PyFile_WriteString("\n", f);
1167 if (offset == -1)
1168 return;
1169 PyFile_WriteString(" ", f);
1170 offset--;
1171 while (offset > 0) {
1172 PyFile_WriteString(" ", f);
1173 offset--;
1174 }
1175 PyFile_WriteString("^\n", f);
1176}
1177
Guido van Rossum66e8e862001-03-23 17:54:43 +00001178static void
1179handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001180{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001181 PyObject *exception, *value, *tb;
1182 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001183
Guido van Rossumd8faa362007-04-27 19:54:29 +00001184 if (Py_InspectFlag)
1185 /* Don't exit if -i flag was given. This flag is set to 0
1186 * when entering interactive mode for inspecting. */
1187 return;
1188
Guido van Rossum66e8e862001-03-23 17:54:43 +00001189 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001190 fflush(stdout);
1191 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001192 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001193 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001194 /* The error code should be in the `code' attribute. */
1195 PyObject *code = PyObject_GetAttrString(value, "code");
1196 if (code) {
1197 Py_DECREF(value);
1198 value = code;
1199 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001200 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001201 }
1202 /* If we failed to dig out the 'code' attribute,
1203 just let the else clause below print the error. */
1204 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001205 if (PyLong_Check(value))
1206 exitcode = (int)PyLong_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001207 else {
1208 PyObject_Print(value, stderr, Py_PRINT_RAW);
1209 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001210 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001211 }
Tim Peterscf615b52003-04-19 18:47:02 +00001212 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001213 /* Restore and clear the exception info, in order to properly decref
1214 * the exception, value, and traceback. If we just exit instead,
1215 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1216 * some finalizers from running.
1217 */
Tim Peterscf615b52003-04-19 18:47:02 +00001218 PyErr_Restore(exception, value, tb);
1219 PyErr_Clear();
1220 Py_Exit(exitcode);
1221 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001222}
1223
1224void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001225PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001226{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001227 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001228
1229 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1230 handle_system_exit();
1231 }
Guido van Rossum82598051997-03-05 00:20:32 +00001232 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 if (exception == NULL)
1234 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001235 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001236 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001237 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001238 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001239 if (set_sys_last_vars) {
1240 PySys_SetObject("last_type", exception);
1241 PySys_SetObject("last_value", v);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001242 PySys_SetObject("last_traceback", tb ? tb : Py_None);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001243 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001244 hook = PySys_GetObject("excepthook");
1245 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001246 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001247 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001248 PyObject *result = PyEval_CallObject(hook, args);
1249 if (result == NULL) {
1250 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001251 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1252 handle_system_exit();
1253 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001254 PyErr_Fetch(&exception2, &v2, &tb2);
1255 PyErr_NormalizeException(&exception2, &v2, &tb2);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001256 /* It should not be possible for exception2 or v2
1257 to be NULL. However PyErr_Display() can't
1258 tolerate NULLs, so just be safe. */
1259 if (exception2 == NULL) {
1260 exception2 = Py_None;
1261 Py_INCREF(exception2);
1262 }
1263 if (v2 == NULL) {
1264 v2 = Py_None;
1265 Py_INCREF(v2);
1266 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001267 fflush(stdout);
1268 PySys_WriteStderr("Error in sys.excepthook:\n");
1269 PyErr_Display(exception2, v2, tb2);
1270 PySys_WriteStderr("\nOriginal exception was:\n");
1271 PyErr_Display(exception, v, tb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001272 Py_DECREF(exception2);
1273 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001274 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001275 }
1276 Py_XDECREF(result);
1277 Py_XDECREF(args);
1278 } else {
1279 PySys_WriteStderr("sys.excepthook is missing\n");
1280 PyErr_Display(exception, v, tb);
1281 }
1282 Py_XDECREF(exception);
1283 Py_XDECREF(v);
1284 Py_XDECREF(tb);
1285}
1286
Thomas Wouters477c8d52006-05-27 19:21:47 +00001287void
1288PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001289{
1290 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001291 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001292 Py_INCREF(value);
Christian Heimes2be03732007-11-15 02:26:46 +00001293 if (f == Py_None) {
1294 /* pass */
1295 }
1296 else if (f == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001297 _PyObject_Dump(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001298 fprintf(stderr, "lost sys.stderr\n");
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001299 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001300 else {
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001301 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001302 if (tb && tb != Py_None)
1303 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001304 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001305 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001306 {
Guido van Rossum82598051997-03-05 00:20:32 +00001307 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001308 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001309 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001310 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001311 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001312 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001313 else {
1314 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001315 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001316 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001317 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001318 else
Guido van Rossum82598051997-03-05 00:20:32 +00001319 PyFile_WriteString(filename, f);
1320 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001321 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001322 PyFile_WriteString(buf, f);
1323 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001324 if (text != NULL)
1325 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001326 Py_DECREF(value);
1327 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001328 /* Can't be bothered to check all those
1329 PyFile_WriteString() calls */
1330 if (PyErr_Occurred())
1331 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001332 }
1333 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001334 if (err) {
1335 /* Don't do anything else */
1336 }
Brett Cannonbf364092006-03-01 04:25:17 +00001337 else if (PyExceptionClass_Check(exception)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001338 PyObject* moduleName;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001339 char* className = PyExceptionClass_Name(exception);
1340 if (className != NULL) {
1341 char *dot = strrchr(className, '.');
1342 if (dot != NULL)
1343 className = dot+1;
1344 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001345
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001346 moduleName = PyObject_GetAttrString(exception, "__module__");
Guido van Rossumc5724de2007-10-10 21:38:59 +00001347 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1348 {
1349 Py_DECREF(moduleName);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001350 err = PyFile_WriteString("<unknown>", f);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001351 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001352 else {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001353 char* modstr = PyUnicode_AsString(moduleName);
Georg Brandl1a3284e2007-12-02 09:40:06 +00001354 if (modstr && strcmp(modstr, "builtins"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001355 {
1356 err = PyFile_WriteString(modstr, f);
1357 err += PyFile_WriteString(".", f);
1358 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001360 }
1361 if (err == 0) {
1362 if (className == NULL)
1363 err = PyFile_WriteString("<unknown>", f);
1364 else
Brett Cannonbf364092006-03-01 04:25:17 +00001365 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001366 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001367 }
1368 else
1369 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001370 if (err == 0 && (value != Py_None)) {
Thomas Heller519a0422007-11-15 20:48:54 +00001371 PyObject *s = PyObject_Str(value);
Brett Cannon2e63b732006-03-02 18:34:57 +00001372 /* only print colon if the str() of the
1373 object is not the empty string
1374 */
1375 if (s == NULL)
1376 err = -1;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001377 else if (!PyUnicode_Check(s) ||
1378 PyUnicode_GetSize(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001379 err = PyFile_WriteString(": ", f);
1380 if (err == 0)
1381 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1382 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001383 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001384 /* try to write a newline in any case */
1385 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001386 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001387 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001388 /* If an error happened here, don't show it.
1389 XXX This is wrong, but too many callers rely on this behavior. */
1390 if (err != 0)
1391 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001392}
1393
Guido van Rossum82598051997-03-05 00:20:32 +00001394PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001395PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001397{
Neal Norwitze92fba02006-03-04 18:52:26 +00001398 PyObject *ret = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001399 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001400 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001401 if (arena == NULL)
1402 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001403
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001404 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001405 if (mod != NULL)
1406 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001407 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001409}
1410
1411PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001412PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001413 PyObject *locals, int closeit, PyCompilerFlags *flags)
1414{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 PyObject *ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001416 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001417 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001418 if (arena == NULL)
1419 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001420
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001421 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001422 flags, NULL, arena);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001423 if (closeit)
1424 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001425 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001426 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001428 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001429 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001430 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001432}
1433
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001434static void
1435flush_io(void)
1436{
1437 PyObject *f, *r;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001438 PyObject *type, *value, *traceback;
1439
1440 /* Save the current exception */
1441 PyErr_Fetch(&type, &value, &traceback);
1442
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001443 f = PySys_GetObject("stderr");
1444 if (f != NULL) {
1445 r = PyObject_CallMethod(f, "flush", "");
1446 if (r)
1447 Py_DECREF(r);
1448 else
1449 PyErr_Clear();
1450 }
1451 f = PySys_GetObject("stdout");
1452 if (f != NULL) {
1453 r = PyObject_CallMethod(f, "flush", "");
1454 if (r)
1455 Py_DECREF(r);
1456 else
1457 PyErr_Clear();
1458 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001459
1460 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001461}
1462
Guido van Rossum82598051997-03-05 00:20:32 +00001463static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001465 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001466{
Guido van Rossum82598051997-03-05 00:20:32 +00001467 PyCodeObject *co;
1468 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001470 if (co == NULL)
1471 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001472 v = PyEval_EvalCode(co, globals, locals);
1473 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001474 return v;
1475}
1476
Guido van Rossum82598051997-03-05 00:20:32 +00001477static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001478run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001480{
Guido van Rossum82598051997-03-05 00:20:32 +00001481 PyCodeObject *co;
1482 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001483 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001484 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001485
Guido van Rossum82598051997-03-05 00:20:32 +00001486 magic = PyMarshal_ReadLongFromFile(fp);
1487 if (magic != PyImport_GetMagicNumber()) {
1488 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001489 "Bad magic number in .pyc file");
1490 return NULL;
1491 }
Guido van Rossum82598051997-03-05 00:20:32 +00001492 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001493 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001494 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001495 if (v == NULL || !PyCode_Check(v)) {
1496 Py_XDECREF(v);
1497 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001498 "Bad code object in .pyc file");
1499 return NULL;
1500 }
Guido van Rossum82598051997-03-05 00:20:32 +00001501 co = (PyCodeObject *)v;
1502 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001503 if (v && flags)
1504 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001505 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001506 return v;
1507}
1508
Guido van Rossum82598051997-03-05 00:20:32 +00001509PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001510Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001511 PyCompilerFlags *flags)
1512{
Guido van Rossum82598051997-03-05 00:20:32 +00001513 PyCodeObject *co;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001514 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001515 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001516 if (arena == NULL)
1517 return NULL;
1518
1519 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001520 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001521 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001522 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001523 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001524 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001525 PyObject *result = PyAST_mod2obj(mod);
1526 PyArena_Free(arena);
1527 return result;
1528 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001529 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001530 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001531 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001532}
1533
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001534struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001535Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001536{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001537 struct symtable *st;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001538 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001539 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001540 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001541 if (arena == NULL)
1542 return NULL;
1543
Christian Heimesb1b3efc2008-03-26 23:24:27 +00001544 flags.cf_flags = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001545 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001546 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001547 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001548 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001551 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001552 return st;
1553}
1554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555/* Preferred access to parser is through AST. */
1556mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001557PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001558 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 mod_ty mod;
1561 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001562 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001563
1564 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001565 &_PyParser_Grammar, start, &err,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001566 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001568 if (flags) {
1569 flags->cf_flags |= iflags & PyCF_MASK;
1570 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001571 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 PyNode_Free(n);
1573 return mod;
1574 }
1575 else {
1576 err_input(&err);
1577 return NULL;
1578 }
1579}
1580
1581mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001582PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1583 int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001584 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001585 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 mod_ty mod;
1588 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001589 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001590
Christian Heimes4d6ec852008-03-26 22:34:47 +00001591 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001592 &_PyParser_Grammar,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001593 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001595 if (flags) {
1596 flags->cf_flags |= iflags & PyCF_MASK;
1597 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001598 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 PyNode_Free(n);
1600 return mod;
1601 }
1602 else {
1603 err_input(&err);
1604 if (errcode)
1605 *errcode = err.error;
1606 return NULL;
1607 }
1608}
1609
Guido van Rossuma110aa61994-08-29 12:50:44 +00001610/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001611
Guido van Rossuma110aa61994-08-29 12:50:44 +00001612node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001613PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001614{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001615 perrdetail err;
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001616 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1617 &_PyParser_Grammar,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001618 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001619 if (n == NULL)
1620 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001621
Guido van Rossuma110aa61994-08-29 12:50:44 +00001622 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001623}
1624
Guido van Rossuma110aa61994-08-29 12:50:44 +00001625/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001626
Guido van Rossuma110aa61994-08-29 12:50:44 +00001627node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001628PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001629{
Tim Petersfe2127d2001-07-16 05:37:24 +00001630 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001631 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1632 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001633 if (n == NULL)
1634 err_input(&err);
1635 return n;
1636}
1637
1638node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001639PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001640 int start, int flags)
1641{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001642 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001643 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1644 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001645 if (n == NULL)
1646 err_input(&err);
1647 return n;
1648}
1649
1650node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001651PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001652{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001653 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001654}
1655
Guido van Rossum66ebd912003-04-17 16:02:26 +00001656/* May want to move a more generalized form of this to parsetok.c or
1657 even parser modules. */
1658
1659void
1660PyParser_SetError(perrdetail *err)
1661{
1662 err_input(err);
1663}
1664
Guido van Rossuma110aa61994-08-29 12:50:44 +00001665/* Set the error appropriate to the given input error code (see errcode.h) */
1666
1667static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001668err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001669{
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001670 PyObject *v, *w, *errtype, *errtext;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001671 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001672 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001673 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001674 switch (err->error) {
1675 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001676 errtype = PyExc_IndentationError;
1677 if (err->expected == INDENT)
1678 msg = "expected an indented block";
1679 else if (err->token == INDENT)
1680 msg = "unexpected indent";
1681 else if (err->token == DEDENT)
1682 msg = "unexpected unindent";
1683 else {
1684 errtype = PyExc_SyntaxError;
1685 msg = "invalid syntax";
1686 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001687 break;
1688 case E_TOKEN:
1689 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001690 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001691 case E_EOFS:
1692 msg = "EOF while scanning triple-quoted string";
1693 break;
1694 case E_EOLS:
1695 msg = "EOL while scanning single-quoted string";
1696 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001697 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001698 if (!PyErr_Occurred())
1699 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001700 return;
1701 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001702 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001703 return;
1704 case E_EOF:
1705 msg = "unexpected EOF while parsing";
1706 break;
Fred Drake85f36392000-07-11 17:53:00 +00001707 case E_TABSPACE:
1708 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001709 msg = "inconsistent use of tabs and spaces in indentation";
1710 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001711 case E_OVERFLOW:
1712 msg = "expression too long";
1713 break;
Fred Drake85f36392000-07-11 17:53:00 +00001714 case E_DEDENT:
1715 errtype = PyExc_IndentationError;
1716 msg = "unindent does not match any outer indentation level";
1717 break;
1718 case E_TOODEEP:
1719 errtype = PyExc_IndentationError;
1720 msg = "too many levels of indentation";
1721 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001722 case E_DECODE: {
1723 PyObject *type, *value, *tb;
1724 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001725 if (value != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +00001726 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001727 if (u != NULL) {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001728 msg = PyUnicode_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001729 }
1730 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001731 if (msg == NULL)
1732 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001733 Py_XDECREF(type);
1734 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001735 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001736 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001737 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001738 case E_LINECONT:
1739 msg = "unexpected character after line continuation character";
1740 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001741
1742 case E_IDENTIFIER:
1743 msg = "invalid character in identifier";
1744 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001745 default:
1746 fprintf(stderr, "error=%d\n", err->error);
1747 msg = "unknown parsing error";
1748 break;
1749 }
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001750 /* err->text may not be UTF-8 in case of decoding errors.
1751 Explicitly convert to an object. */
1752 if (!err->text) {
1753 errtext = Py_None;
1754 Py_INCREF(Py_None);
1755 } else {
1756 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1757 "replace");
1758 }
1759 v = Py_BuildValue("(ziiN)", err->filename,
1760 err->lineno, err->offset, errtext);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001761 if (err->text != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 err->text = NULL;
1764 }
1765 w = NULL;
1766 if (v != NULL)
1767 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001768 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001769 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001770 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001771 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001772}
1773
1774/* Print fatal error message and abort */
1775
1776void
Tim Peters7c321a82002-07-09 02:57:01 +00001777Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001778{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001779 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001780 if (PyErr_Occurred()) {
1781 PyErr_Print();
1782 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001783#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001784 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001785 OutputDebugString(msg);
1786 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001787#ifdef _DEBUG
1788 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001789#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001790#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001791 abort();
1792}
1793
1794/* Clean up and exit */
1795
Guido van Rossuma110aa61994-08-29 12:50:44 +00001796#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001797#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001798#endif
1799
Collin Winter670e6922007-03-21 02:57:17 +00001800static void (*pyexitfunc)(void) = NULL;
1801/* For the atexit module. */
1802void _Py_PyAtExit(void (*func)(void))
1803{
1804 pyexitfunc = func;
1805}
1806
1807static void
1808call_py_exitfuncs(void)
1809{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001810 if (pyexitfunc == NULL)
Collin Winter670e6922007-03-21 02:57:17 +00001811 return;
1812
1813 (*pyexitfunc)();
1814 PyErr_Clear();
1815}
1816
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001817#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001818static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001819static int nexitfuncs = 0;
1820
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001821int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001822{
1823 if (nexitfuncs >= NEXITFUNCS)
1824 return -1;
1825 exitfuncs[nexitfuncs++] = func;
1826 return 0;
1827}
1828
Guido van Rossumcc283f51997-08-05 02:22:03 +00001829static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001830call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001831{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001832 while (nexitfuncs > 0)
1833 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001834
1835 fflush(stdout);
1836 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001837}
1838
1839void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001840Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001841{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001842 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001843
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001844 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001845}
1846
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001847static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001848initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001849{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001850#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001851 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001852#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001853#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001854 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001855#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001856#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001857 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001858#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001859 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001860}
1861
Guido van Rossum7433b121997-02-14 19:45:36 +00001862
1863/*
1864 * The file descriptor fd is considered ``interactive'' if either
1865 * a) isatty(fd) is TRUE, or
1866 * b) the -i flag was given, and the filename associated with
1867 * the descriptor is NULL or "<stdin>" or "???".
1868 */
1869int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001870Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001871{
1872 if (isatty((int)fileno(fp)))
1873 return 1;
1874 if (!Py_InteractiveFlag)
1875 return 0;
1876 return (filename == NULL) ||
1877 (strcmp(filename, "<stdin>") == 0) ||
1878 (strcmp(filename, "???") == 0);
1879}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001880
1881
Tim Petersd08e3822003-04-17 15:24:21 +00001882#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001883#if defined(WIN32) && defined(_MSC_VER)
1884
1885/* Stack checking for Microsoft C */
1886
1887#include <malloc.h>
1888#include <excpt.h>
1889
Fred Drakee8de31c2000-08-31 05:38:39 +00001890/*
1891 * Return non-zero when we run out of memory on the stack; zero otherwise.
1892 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001893int
Fred Drake399739f2000-08-31 05:52:44 +00001894PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001895{
1896 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001897 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001898 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001899 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001900 return 0;
Christian Heimes7131fd92008-02-19 14:21:46 +00001901 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
Trent Nelson39e307e2008-03-19 06:45:48 +00001902 EXCEPTION_EXECUTE_HANDLER :
1903 EXCEPTION_CONTINUE_SEARCH) {
Christian Heimes7131fd92008-02-19 14:21:46 +00001904 int errcode = _resetstkoflw();
1905 if (errcode)
1906 {
1907 Py_FatalError("Could not reset the stack!");
1908 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001909 }
1910 return 1;
1911}
1912
1913#endif /* WIN32 && _MSC_VER */
1914
1915/* Alternate implementations can be added here... */
1916
1917#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001918
1919
1920/* Wrappers around sigaction() or signal(). */
1921
1922PyOS_sighandler_t
1923PyOS_getsig(int sig)
1924{
1925#ifdef HAVE_SIGACTION
1926 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001927 if (sigaction(sig, NULL, &context) == -1)
1928 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001929 return context.sa_handler;
1930#else
1931 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001932/* Special signal handling for the secure CRT in Visual Studio 2005 */
1933#if defined(_MSC_VER) && _MSC_VER >= 1400
1934 switch (sig) {
1935 /* Only these signals are valid */
1936 case SIGINT:
1937 case SIGILL:
1938 case SIGFPE:
1939 case SIGSEGV:
1940 case SIGTERM:
1941 case SIGBREAK:
1942 case SIGABRT:
1943 break;
1944 /* Don't call signal() with other values or it will assert */
1945 default:
1946 return SIG_ERR;
1947 }
1948#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001949 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001950 if (handler != SIG_ERR)
1951 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001952 return handler;
1953#endif
1954}
1955
1956PyOS_sighandler_t
1957PyOS_setsig(int sig, PyOS_sighandler_t handler)
1958{
1959#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001960 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001961 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001962 sigemptyset(&context.sa_mask);
1963 context.sa_flags = 0;
1964 if (sigaction(sig, &context, &ocontext) == -1)
1965 return SIG_ERR;
1966 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001967#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001968 PyOS_sighandler_t oldhandler;
1969 oldhandler = signal(sig, handler);
1970#ifdef HAVE_SIGINTERRUPT
1971 siginterrupt(sig, 1);
1972#endif
1973 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001974#endif
1975}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976
1977/* Deprecated C API functions still provided for binary compatiblity */
1978
1979#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001980PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1982{
1983 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1984}
1985
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986#undef PyParser_SimpleParseString
1987PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988PyParser_SimpleParseString(const char *str, int start)
1989{
1990 return PyParser_SimpleParseStringFlags(str, start, 0);
1991}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001992
1993#undef PyRun_AnyFile
1994PyAPI_FUNC(int)
1995PyRun_AnyFile(FILE *fp, const char *name)
1996{
1997 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1998}
1999
2000#undef PyRun_AnyFileEx
2001PyAPI_FUNC(int)
2002PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2003{
2004 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
2005}
2006
2007#undef PyRun_AnyFileFlags
2008PyAPI_FUNC(int)
2009PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2010{
2011 return PyRun_AnyFileExFlags(fp, name, 0, flags);
2012}
2013
2014#undef PyRun_File
2015PyAPI_FUNC(PyObject *)
2016PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2017{
2018 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2019}
2020
2021#undef PyRun_FileEx
2022PyAPI_FUNC(PyObject *)
2023PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2024{
2025 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2026}
2027
2028#undef PyRun_FileFlags
2029PyAPI_FUNC(PyObject *)
2030PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2031 PyCompilerFlags *flags)
2032{
2033 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2034}
2035
2036#undef PyRun_SimpleFile
2037PyAPI_FUNC(int)
2038PyRun_SimpleFile(FILE *f, const char *p)
2039{
2040 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2041}
2042
2043#undef PyRun_SimpleFileEx
2044PyAPI_FUNC(int)
2045PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2046{
2047 return PyRun_SimpleFileExFlags(f, p, c, NULL);
2048}
2049
2050
2051#undef PyRun_String
2052PyAPI_FUNC(PyObject *)
2053PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2054{
2055 return PyRun_StringFlags(str, s, g, l, NULL);
2056}
2057
2058#undef PyRun_SimpleString
2059PyAPI_FUNC(int)
2060PyRun_SimpleString(const char *s)
2061{
2062 return PyRun_SimpleStringFlags(s, NULL);
2063}
2064
2065#undef Py_CompileString
2066PyAPI_FUNC(PyObject *)
2067Py_CompileString(const char *str, const char *p, int s)
2068{
2069 return Py_CompileStringFlags(str, p, s, NULL);
2070}
2071
2072#undef PyRun_InteractiveOne
2073PyAPI_FUNC(int)
2074PyRun_InteractiveOne(FILE *f, const char *p)
2075{
2076 return PyRun_InteractiveOneFlags(f, p, NULL);
2077}
2078
2079#undef PyRun_InteractiveLoop
2080PyAPI_FUNC(int)
2081PyRun_InteractiveLoop(FILE *f, const char *p)
2082{
2083 return PyRun_InteractiveLoopFlags(f, p, NULL);
2084}
2085
2086#ifdef __cplusplus
2087}
2088#endif