blob: 24517e4764f4909b9b1c98f9f3449c7d13c66ceb [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000020#include "osdefs.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000025
Martin v. Löwis73d538b2003-03-05 15:13:47 +000026#ifdef HAVE_LANGINFO_H
27#include <locale.h>
28#include <langinfo.h>
29#endif
30
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000031#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000032#undef BYTE
33#include "windows.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000034#define PATH_MAX MAXPATHLEN
Guido van Rossuma44823b1995-03-14 15:01:17 +000035#endif
36
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000039#else /* Py_REF_DEBUG */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040#define PRINT_TOTAL_REFS() fprintf(stderr, \
41 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
42 _Py_GetRefTotal())
43#endif
44
45#ifdef __cplusplus
46extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000047#endif
48
Martin v. Löwis790465f2008-04-05 20:41:37 +000049extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossum82598051997-03-05 00:20:32 +000051extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052
Guido van Rossumb73cc041993-11-01 16:28:59 +000053/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000054static void initmain(void);
55static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000056static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000057static void flush_io(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000059 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000060static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000061 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void err_input(perrdetail *);
63static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000064static void call_py_exitfuncs(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000065static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066extern void _PyUnicode_Init(void);
67extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000068extern int _PyLong_Init(void);
69extern void PyLong_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000070
Mark Hammond8d98d2c2003-04-19 15:41:53 +000071#ifdef WITH_THREAD
72extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73extern void _PyGILState_Fini(void);
74#endif /* WITH_THREAD */
75
Guido van Rossum82598051997-03-05 00:20:32 +000076int Py_DebugFlag; /* Needed by parser.c */
77int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000078int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumd8faa362007-04-27 19:54:29 +000079int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000080int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000081int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +000082int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000083int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000084int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000085int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +000086int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000087
Christian Heimes33fe8092008-04-13 13:53:33 +000088/* PyModule_GetWarningsModule is no longer necessary as of 2.6
89since _warnings is builtin. This API should not be used. */
90PyObject *
91PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000092{
Christian Heimes33fe8092008-04-13 13:53:33 +000093 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000094}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000095
Guido van Rossum25ce5661997-08-02 03:10:38 +000096static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000097
Thomas Wouters7e474022000-07-16 12:04:32 +000098/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000099
100int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000102{
103 return initialized;
104}
105
Guido van Rossum25ce5661997-08-02 03:10:38 +0000106/* Global initializations. Can be undone by Py_Finalize(). Don't
107 call this twice without an intervening Py_Finalize() call. When
108 initializations fail, a fatal error is issued and the function does
109 not return. On return, the first thread and interpreter state have
110 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000111
Guido van Rossum25ce5661997-08-02 03:10:38 +0000112 Locking: you must hold the interpreter lock while calling this.
113 (If the lock has not yet been initialized, that's equivalent to
114 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000115
Guido van Rossum25ce5661997-08-02 03:10:38 +0000116*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000117
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000118static int
119add_flag(int flag, const char *envs)
120{
121 int env = atoi(envs);
122 if (flag < env)
123 flag = env;
124 if (flag < 1)
125 flag = 1;
126 return flag;
127}
128
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000130Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 PyThreadState *tstate;
Guido van Rossum826d8972007-10-30 18:34:07 +0000134 PyObject *bimod, *sysmod, *pstderr;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000135 char *p;
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000136#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000137 char *codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000138#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000139 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000140
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000141 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000142 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000143 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000144
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000145#ifdef HAVE_SETLOCALE
146 /* Set up the LC_CTYPE locale, so we can obtain
147 the locale's charset without having to switch
148 locales. */
149 setlocale(LC_CTYPE, "");
150#endif
151
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000152 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000153 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000154 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000155 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000156 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000157 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Christian Heimes790c8232008-01-07 21:14:23 +0000158 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
159 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000160
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161 interp = PyInterpreterState_New();
162 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000163 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000164
Guido van Rossuma027efa1997-05-05 20:56:21 +0000165 tstate = PyThreadState_New(interp);
166 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000167 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 (void) PyThreadState_Swap(tstate);
169
Guido van Rossum70d893a2001-08-16 08:21:42 +0000170 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000171
Neal Norwitzb2501f42002-12-31 03:42:13 +0000172 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000173 Py_FatalError("Py_Initialize: can't init frames");
174
Guido van Rossumddefaf32007-01-14 03:31:43 +0000175 if (!_PyLong_Init())
176 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000177
Christian Heimes9c4756e2008-05-26 13:22:05 +0000178 if (!PyByteArray_Init())
Neal Norwitz6968b052007-02-27 19:02:19 +0000179 Py_FatalError("Py_Initialize: can't init bytes");
180
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000181 _PyFloat_Init();
182
Guido van Rossum25ce5661997-08-02 03:10:38 +0000183 interp->modules = PyDict_New();
184 if (interp->modules == NULL)
185 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000186 interp->modules_reloading = PyDict_New();
187 if (interp->modules_reloading == NULL)
188 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000189
Guido van Rossumc94044c2000-03-10 23:03:54 +0000190 /* Init Unicode implementation; relies on the codec registry */
191 _PyUnicode_Init();
192
Barry Warsawf242aa02000-05-25 23:09:49 +0000193 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194 if (bimod == NULL)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000195 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000196 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000197 if (interp->builtins == NULL)
198 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000199 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000201 /* initialize builtin exceptions */
202 _PyExc_Init();
203
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204 sysmod = _PySys_Init();
205 if (sysmod == NULL)
206 Py_FatalError("Py_Initialize: can't initialize sys");
207 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000208 if (interp->sysdict == NULL)
209 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000210 Py_INCREF(interp->sysdict);
211 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000212 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 PyDict_SetItemString(interp->sysdict, "modules",
214 interp->modules);
215
Guido van Rossum826d8972007-10-30 18:34:07 +0000216 /* Set up a preliminary stderr printer until we have enough
217 infrastructure for the io module in place. */
218 pstderr = PyFile_NewStdPrinter(fileno(stderr));
219 if (pstderr == NULL)
220 Py_FatalError("Py_Initialize: can't set preliminary stderr");
221 PySys_SetObject("stderr", pstderr);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000222 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000223
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000224 _PyImport_Init();
225
Barry Warsaw035574d1997-08-29 22:07:17 +0000226 /* phase 2 of builtins */
Georg Brandl1a3284e2007-12-02 09:40:06 +0000227 _PyImport_FixupExtension("builtins", "builtins");
Barry Warsaw035574d1997-08-29 22:07:17 +0000228
Just van Rossum52e14d62002-12-30 22:08:05 +0000229 _PyImportHooks_Init();
230
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000231 if (install_sigs)
232 initsigs(); /* Signal handling stuff, including initintr() */
Christian Heimes33fe8092008-04-13 13:53:33 +0000233
234 /* Initialize warnings. */
235 _PyWarnings_Init();
236 if (PySys_HasWarnOptions()) {
237 PyObject *warnings_module = PyImport_ImportModule("warnings");
238 if (!warnings_module)
239 PyErr_Clear();
240 Py_XDECREF(warnings_module);
241 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000242
243 initmain(); /* Module __main__ */
Alexandre Vassalotti77250f42008-05-06 19:48:38 +0000244 if (!Py_NoSiteFlag)
245 initsite(); /* Module site */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000246 if (initstdio() < 0)
247 Py_FatalError(
248 "Py_Initialize: can't initialize sys standard streams");
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000249
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000250 /* auto-thread-state API, if available */
251#ifdef WITH_THREAD
252 _PyGILState_Init(interp, tstate);
253#endif /* WITH_THREAD */
254
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000255#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000256 /* On Unix, set the file system encoding according to the
257 user's preference, if the CODESET names a well-known
258 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000259 initialized by other means. Also set the encoding of
260 stdin and stdout if these are terminals. */
261
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000262 codeset = nl_langinfo(CODESET);
263 if (codeset && *codeset) {
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000264 if (PyCodec_KnownEncoding(codeset))
265 codeset = strdup(codeset);
266 else
267 codeset = NULL;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000268 } else
269 codeset = NULL;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000270
271 if (codeset) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000272 if (!Py_FileSystemDefaultEncoding)
273 Py_FileSystemDefaultEncoding = codeset;
274 else
275 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000276 }
277#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000278}
279
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000280void
281Py_Initialize(void)
282{
283 Py_InitializeEx(1);
284}
285
286
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000287#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000288extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000289#endif
290
Guido van Rossume8432ac2007-07-09 15:04:50 +0000291/* Flush stdout and stderr */
292
Neal Norwitz2bad9702007-08-27 06:19:22 +0000293static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000294flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000295{
296 PyObject *fout = PySys_GetObject("stdout");
297 PyObject *ferr = PySys_GetObject("stderr");
298 PyObject *tmp;
299
Christian Heimes2be03732007-11-15 02:26:46 +0000300 if (fout != NULL && fout != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000301 tmp = PyObject_CallMethod(fout, "flush", "");
302 if (tmp == NULL)
303 PyErr_Clear();
304 else
305 Py_DECREF(tmp);
306 }
307
Christian Heimes2be03732007-11-15 02:26:46 +0000308 if (ferr != NULL || ferr != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000309 tmp = PyObject_CallMethod(ferr, "flush", "");
310 if (tmp == NULL)
311 PyErr_Clear();
312 else
313 Py_DECREF(tmp);
314 }
315}
316
Guido van Rossum25ce5661997-08-02 03:10:38 +0000317/* Undo the effect of Py_Initialize().
318
319 Beware: if multiple interpreter and/or thread states exist, these
320 are not wiped out; only the current thread and interpreter state
321 are deleted. But since everything else is deleted, those other
322 interpreter and thread states should no longer be used.
323
324 (XXX We should do better, e.g. wipe out all interpreters and
325 threads.)
326
327 Locking: as above.
328
329*/
330
331void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333{
334 PyInterpreterState *interp;
335 PyThreadState *tstate;
336
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000337 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000338 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000339
Tim Peters384fd102001-01-21 03:40:37 +0000340 /* The interpreter is still entirely intact at this point, and the
341 * exit funcs may be relying on that. In particular, if some thread
342 * or exit func is still waiting to do an import, the import machinery
343 * expects Py_IsInitialized() to return true. So don't say the
344 * interpreter is uninitialized until after the exit funcs have run.
345 * Note that Threading.py uses an exit func to do a join on all the
346 * threads created thru it, so this also protects pending imports in
347 * the threads created via Threading.
348 */
Collin Winter670e6922007-03-21 02:57:17 +0000349 call_py_exitfuncs();
Tim Peters384fd102001-01-21 03:40:37 +0000350 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000351
Guido van Rossume8432ac2007-07-09 15:04:50 +0000352 /* Flush stdout+stderr */
353 flush_std_files();
354
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000355 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000356 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357 interp = tstate->interp;
358
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000359 /* Disable signal handling */
360 PyOS_FiniInterrupts();
361
Christian Heimes26855632008-01-27 23:50:43 +0000362 /* Clear type lookup cache */
363 PyType_ClearCache();
364
Guido van Rossume13ddc92003-04-17 17:29:22 +0000365 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000366 * before all modules are destroyed.
367 * XXX If a __del__ or weakref callback is triggered here, and tries to
368 * XXX import a module, bad things can happen, because Python no
369 * XXX longer believes it's initialized.
370 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
371 * XXX is easy to provoke that way. I've also seen, e.g.,
372 * XXX Exception exceptions.ImportError: 'No module named sha'
373 * XXX in <function callback at 0x008F5718> ignored
374 * XXX but I'm unclear on exactly how that one happens. In any case,
375 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000376 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000377 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000378#ifdef COUNT_ALLOCS
379 /* With COUNT_ALLOCS, it helps to run GC multiple times:
380 each collection might release some types from the type
381 list, so they become garbage. */
382 while (PyGC_Collect() > 0)
383 /* nothing */;
384#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000385
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000386 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000387 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000388
Guido van Rossume8432ac2007-07-09 15:04:50 +0000389 /* Flush stdout+stderr (again, in case more was printed) */
390 flush_std_files();
391
Guido van Rossume13ddc92003-04-17 17:29:22 +0000392 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000393 * new-style class definitions, for example.
394 * XXX This is disabled because it caused too many problems. If
395 * XXX a __del__ or weakref callback triggers here, Python code has
396 * XXX a hard time running, because even the sys module has been
397 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
398 * XXX One symptom is a sequence of information-free messages
399 * XXX coming from threads (if a __del__ or callback is invoked,
400 * XXX other threads can execute too, and any exception they encounter
401 * XXX triggers a comedy of errors as subsystem after subsystem
402 * XXX fails to find what it *expects* to find in sys to help report
403 * XXX the exception and consequent unexpected failures). I've also
404 * XXX seen segfaults then, after adding print statements to the
405 * XXX Python code getting called.
406 */
407#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000408 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000409#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000410
Guido van Rossum1707aad1997-12-08 23:43:45 +0000411 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
412 _PyImport_Fini();
413
414 /* Debugging stuff */
415#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000417#endif
418
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000419 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000420
Tim Peters9cf25ce2003-04-17 15:21:01 +0000421#ifdef Py_TRACE_REFS
422 /* Display all objects still alive -- this can invoke arbitrary
423 * __repr__ overrides, so requires a mostly-intact interpreter.
424 * Alas, a lot of stuff may still be alive now that will be cleaned
425 * up later.
426 */
Tim Peters269b2a62003-04-17 19:52:29 +0000427 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000428 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000429#endif /* Py_TRACE_REFS */
430
Guido van Rossumd922fa42003-04-15 14:10:09 +0000431 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000432 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000433
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000434 /* Now we decref the exception classes. After this point nothing
435 can raise an exception. That's okay, because each Fini() method
436 below has been checked to make sure no exceptions are ever
437 raised.
438 */
439
440 _PyExc_Fini();
441
Christian Heimes7d2ff882007-11-30 14:35:04 +0000442 /* Cleanup auto-thread-state */
443#ifdef WITH_THREAD
444 _PyGILState_Fini();
445#endif /* WITH_THREAD */
446
Guido van Rossumd922fa42003-04-15 14:10:09 +0000447 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000448 PyThreadState_Swap(NULL);
449 PyInterpreterState_Delete(interp);
450
Guido van Rossumd922fa42003-04-15 14:10:09 +0000451 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000452 PyMethod_Fini();
453 PyFrame_Fini();
454 PyCFunction_Fini();
455 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000456 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000457 PySet_Fini();
Christian Heimes72b710a2008-05-26 13:28:38 +0000458 PyBytes_Fini();
Christian Heimes9c4756e2008-05-26 13:22:05 +0000459 PyByteArray_Fini();
Guido van Rossumddefaf32007-01-14 03:31:43 +0000460 PyLong_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000461 PyFloat_Fini();
Christian Heimes77c02eb2008-02-09 02:18:51 +0000462 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000463
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000464 /* Cleanup Unicode implementation */
465 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000466
Christian Heimesc8967002007-11-30 10:18:26 +0000467 /* reset file system default encoding */
468 if (!Py_HasFileSystemDefaultEncoding) {
469 free((char*)Py_FileSystemDefaultEncoding);
Trent Nelson39e307e2008-03-19 06:45:48 +0000470 Py_FileSystemDefaultEncoding = NULL;
Christian Heimesc8967002007-11-30 10:18:26 +0000471 }
472
Guido van Rossumcc283f51997-08-05 02:22:03 +0000473 /* XXX Still allocated:
474 - various static ad-hoc pointers to interned strings
475 - int and float free list blocks
476 - whatever various modules and libraries allocate
477 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000478
479 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000480
Tim Peters269b2a62003-04-17 19:52:29 +0000481#ifdef Py_TRACE_REFS
482 /* Display addresses (& refcnts) of all objects still alive.
483 * An address can be used to find the repr of the object, printed
484 * above by _Py_PrintReferences.
485 */
486 if (Py_GETENV("PYTHONDUMPREFS"))
487 _Py_PrintReferenceAddresses(stderr);
488#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000489#ifdef PYMALLOC_DEBUG
490 if (Py_GETENV("PYTHONMALLOCSTATS"))
491 _PyObject_DebugMallocStats();
492#endif
493
Guido van Rossumcc283f51997-08-05 02:22:03 +0000494 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000495}
496
497/* Create and initialize a new interpreter and thread, and return the
498 new thread. This requires that Py_Initialize() has been called
499 first.
500
501 Unsuccessful initialization yields a NULL pointer. Note that *no*
502 exception information is available even in this case -- the
503 exception information is held in the thread, and there is no
504 thread.
505
506 Locking: as above.
507
508*/
509
510PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000511Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000512{
513 PyInterpreterState *interp;
514 PyThreadState *tstate, *save_tstate;
515 PyObject *bimod, *sysmod;
516
517 if (!initialized)
518 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
519
520 interp = PyInterpreterState_New();
521 if (interp == NULL)
522 return NULL;
523
524 tstate = PyThreadState_New(interp);
525 if (tstate == NULL) {
526 PyInterpreterState_Delete(interp);
527 return NULL;
528 }
529
530 save_tstate = PyThreadState_Swap(tstate);
531
532 /* XXX The following is lax in error checking */
533
534 interp->modules = PyDict_New();
Guido van Rossumd8faa362007-04-27 19:54:29 +0000535 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000536
Georg Brandl1a3284e2007-12-02 09:40:06 +0000537 bimod = _PyImport_FindExtension("builtins", "builtins");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000538 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000539 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000540 if (interp->builtins == NULL)
541 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000542 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000543 }
544 sysmod = _PyImport_FindExtension("sys", "sys");
545 if (bimod != NULL && sysmod != NULL) {
546 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000547 if (interp->sysdict == NULL)
548 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000549 Py_INCREF(interp->sysdict);
550 PySys_SetPath(Py_GetPath());
551 PyDict_SetItemString(interp->sysdict, "modules",
552 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000553 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000555 if (!Py_NoSiteFlag)
556 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000557 }
558
559 if (!PyErr_Occurred())
560 return tstate;
561
Thomas Wouters89f507f2006-12-13 04:49:30 +0000562handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000563 /* Oops, it didn't work. Undo it all. */
564
565 PyErr_Print();
566 PyThreadState_Clear(tstate);
567 PyThreadState_Swap(save_tstate);
568 PyThreadState_Delete(tstate);
569 PyInterpreterState_Delete(interp);
570
571 return NULL;
572}
573
574/* Delete an interpreter and its last thread. This requires that the
575 given thread state is current, that the thread has no remaining
576 frames, and that it is its interpreter's only remaining thread.
577 It is a fatal error to violate these constraints.
578
579 (Py_Finalize() doesn't have these constraints -- it zaps
580 everything, regardless.)
581
582 Locking: as above.
583
584*/
585
586void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000587Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000588{
589 PyInterpreterState *interp = tstate->interp;
590
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000591 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 Py_FatalError("Py_EndInterpreter: thread is not current");
593 if (tstate->frame != NULL)
594 Py_FatalError("Py_EndInterpreter: thread still has a frame");
595 if (tstate != interp->tstate_head || tstate->next != NULL)
596 Py_FatalError("Py_EndInterpreter: not the last thread");
597
598 PyImport_Cleanup();
599 PyInterpreterState_Clear(interp);
600 PyThreadState_Swap(NULL);
601 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000602}
603
Martin v. Löwis790465f2008-04-05 20:41:37 +0000604static wchar_t *progname = L"python";
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000605
606void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000607Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000608{
609 if (pn && *pn)
610 progname = pn;
611}
612
Martin v. Löwis790465f2008-04-05 20:41:37 +0000613wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000614Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000615{
616 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000617}
618
Martin v. Löwis790465f2008-04-05 20:41:37 +0000619static wchar_t *default_home = NULL;
620static wchar_t env_home[PATH_MAX+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000621
622void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000623Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000624{
625 default_home = home;
626}
627
Martin v. Löwis790465f2008-04-05 20:41:37 +0000628wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000629Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000630{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000631 wchar_t *home = default_home;
632 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
633 char* chome = Py_GETENV("PYTHONHOME");
634 if (chome) {
635 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
636 if (r != (size_t)-1 && r <= PATH_MAX)
637 home = env_home;
638 }
639
640 }
Guido van Rossuma61691e1998-02-06 22:27:24 +0000641 return home;
642}
643
Guido van Rossum6135a871995-01-09 17:53:26 +0000644/* Create __main__ module */
645
646static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000647initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000648{
Guido van Rossum82598051997-03-05 00:20:32 +0000649 PyObject *m, *d;
650 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000651 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000652 Py_FatalError("can't create __main__ module");
653 d = PyModule_GetDict(m);
654 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Georg Brandl1a3284e2007-12-02 09:40:06 +0000655 PyObject *bimod = PyImport_ImportModule("builtins");
Guido van Rossum858cb731997-11-19 16:15:37 +0000656 if (bimod == NULL ||
657 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000658 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000659 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000660 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000661}
662
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000663/* Import the site module (not into __main__ though) */
664
665static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000667{
668 PyObject *m, *f;
669 m = PyImport_ImportModule("site");
670 if (m == NULL) {
671 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000672 if (f == NULL || f == Py_None)
673 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000674 if (Py_VerboseFlag) {
675 PyFile_WriteString(
676 "'import site' failed; traceback:\n", f);
677 PyErr_Print();
678 }
679 else {
680 PyFile_WriteString(
681 "'import site' failed; use -v for traceback\n", f);
682 PyErr_Clear();
683 }
684 }
685 else {
686 Py_DECREF(m);
687 }
688}
689
Georg Brandl1a3284e2007-12-02 09:40:06 +0000690/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000691static int
692initstdio(void)
693{
694 PyObject *iomod = NULL, *wrapper;
695 PyObject *bimod = NULL;
696 PyObject *m;
697 PyObject *std = NULL;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000698 int status = 0, fd;
Trent Nelson39e307e2008-03-19 06:45:48 +0000699 PyObject * encoding_attr;
Martin v. Löwis0f599892008-06-02 11:13:03 +0000700 char *encoding, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000701
702 /* Hack to avoid a nasty recursion issue when Python is invoked
703 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
704 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
705 goto error;
706 }
707 Py_DECREF(m);
708
709 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
710 goto error;
711 }
712 Py_DECREF(m);
713
Georg Brandl1a3284e2007-12-02 09:40:06 +0000714 if (!(bimod = PyImport_ImportModule("builtins"))) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000715 goto error;
716 }
717
718 if (!(iomod = PyImport_ImportModule("io"))) {
719 goto error;
720 }
721 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
722 goto error;
723 }
724
Georg Brandl1a3284e2007-12-02 09:40:06 +0000725 /* Set builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000726 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
727 goto error;
728 }
729
Martin v. Löwis0f599892008-06-02 11:13:03 +0000730 encoding = Py_GETENV("PYTHONIOENCODING");
Martin v. Löwis7cd068b2008-06-02 12:33:47 +0000731 errors = NULL;
Martin v. Löwis0f599892008-06-02 11:13:03 +0000732 if (encoding) {
733 encoding = strdup(encoding);
734 errors = strchr(encoding, ':');
735 if (errors) {
736 *errors = '\0';
737 errors++;
738 }
739 }
740
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000741 /* Set sys.stdin */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000742 fd = fileno(stdin);
743 /* Under some conditions stdin, stdout and stderr may not be connected
744 * and fileno() may point to an invalid file descriptor. For example
745 * GUI apps don't have valid standard streams by default.
746 */
747 if (fd < 0) {
748#ifdef MS_WINDOWS
749 std = Py_None;
750 Py_INCREF(std);
751#else
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000752 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000753#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000754 }
Christian Heimes58cb1b82007-11-13 02:19:40 +0000755 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000756 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding,
757 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000758 goto error;
759 }
760 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000761 PySys_SetObject("__stdin__", std);
762 PySys_SetObject("stdin", std);
763 Py_DECREF(std);
764
765 /* Set sys.stdout */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000766 fd = fileno(stdout);
767 if (fd < 0) {
768#ifdef MS_WINDOWS
769 std = Py_None;
770 Py_INCREF(std);
771#else
772 goto error;
773#endif
774 }
775 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000776 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding,
777 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000778 goto error;
779 }
780 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000781 PySys_SetObject("__stdout__", std);
782 PySys_SetObject("stdout", std);
783 Py_DECREF(std);
784
Guido van Rossum98297ee2007-11-06 21:34:58 +0000785#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000786 /* Set sys.stderr, replaces the preliminary stderr */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000787 fd = fileno(stderr);
788 if (fd < 0) {
789#ifdef MS_WINDOWS
790 std = Py_None;
791 Py_INCREF(std);
792#else
793 goto error;
794#endif
795 }
796 else {
Martin v. Löwis0f599892008-06-02 11:13:03 +0000797 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
798 errors, "\n", 0))) {
Christian Heimes58cb1b82007-11-13 02:19:40 +0000799 goto error;
800 }
801 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +0000802
803 /* Same as hack above, pre-import stderr's codec to avoid recursion
804 when import.c tries to write to stderr in verbose mode. */
805 encoding_attr = PyObject_GetAttrString(std, "encoding");
806 if (encoding_attr != NULL) {
807 const char * encoding;
808 encoding = PyUnicode_AsString(encoding_attr);
809 if (encoding != NULL) {
810 _PyCodec_Lookup(encoding);
811 }
812 }
813 PyErr_Clear(); /* Not a fatal error if codec isn't available */
814
Christian Heimesdb233082007-11-13 02:34:21 +0000815 PySys_SetObject("__stderr__", std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000816 PySys_SetObject("stderr", std);
817 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000818#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000819
Christian Heimes58cb1b82007-11-13 02:19:40 +0000820 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000821 error:
Christian Heimesdb233082007-11-13 02:34:21 +0000822 status = -1;
823 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000824
Martin v. Löwis7cd068b2008-06-02 12:33:47 +0000825 if (encoding)
826 free(encoding);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000827 Py_XDECREF(bimod);
828 Py_XDECREF(iomod);
829 return status;
830}
831
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000832/* Parse input from a file and execute it */
833
834int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000835PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000836 PyCompilerFlags *flags)
837{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000838 if (filename == NULL)
839 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000840 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000841 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000842 if (closeit)
843 fclose(fp);
844 return err;
845 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000846 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000847 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848}
849
850int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000851PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000852{
Guido van Rossum82598051997-03-05 00:20:32 +0000853 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000854 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000855 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000856
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000857 if (flags == NULL) {
858 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000859 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000860 }
Guido van Rossum82598051997-03-05 00:20:32 +0000861 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000862 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000863 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000864 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000865 }
Guido van Rossum82598051997-03-05 00:20:32 +0000866 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000867 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000868 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000869 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000870 }
871 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000872 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000874 if (ret == E_EOF)
875 return 0;
876 /*
877 if (ret == E_NOMEM)
878 return -1;
879 */
880 }
881}
882
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000883/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000884#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000885 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Guido van Rossum45aecf42006-03-15 04:58:47 +0000886 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000887
Thomas Wouters89f507f2006-12-13 04:49:30 +0000888#if 0
889/* Keep an example of flags with future keyword support. */
890#define PARSER_FLAGS(flags) \
891 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
892 PyPARSE_DONT_IMPLY_DEDENT : 0) \
893 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
894 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
895#endif
896
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000897int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000898PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000899{
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000900 PyObject *m, *d, *v, *w, *oenc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000902 PyArena *arena;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000903 char *ps1 = "", *ps2 = "", *enc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000905
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000906 if (fp == stdin) {
907 /* Fetch encoding from sys.stdin */
908 v = PySys_GetObject("stdin");
Christian Heimes2be03732007-11-15 02:26:46 +0000909 if (v == NULL || v == Py_None)
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000910 return -1;
911 oenc = PyObject_GetAttrString(v, "encoding");
912 if (!oenc)
913 return -1;
914 enc = PyUnicode_AsString(oenc);
915 }
Guido van Rossum82598051997-03-05 00:20:32 +0000916 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000917 if (v != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000918 v = PyObject_Str(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000919 if (v == NULL)
920 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000921 else if (PyUnicode_Check(v))
922 ps1 = PyUnicode_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000923 }
Guido van Rossum82598051997-03-05 00:20:32 +0000924 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000925 if (w != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000926 w = PyObject_Str(w);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000927 if (w == NULL)
928 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000929 else if (PyUnicode_Check(w))
930 ps2 = PyUnicode_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000931 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000932 arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000933 if (arena == NULL) {
934 Py_XDECREF(v);
935 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000936 Py_XDECREF(oenc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000937 return -1;
938 }
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000939 mod = PyParser_ASTFromFile(fp, filename, enc,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000941 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000942 Py_XDECREF(v);
943 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000944 Py_XDECREF(oenc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000946 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 if (errcode == E_EOF) {
948 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000949 return E_EOF;
950 }
Guido van Rossum82598051997-03-05 00:20:32 +0000951 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000953 }
Guido van Rossum82598051997-03-05 00:20:32 +0000954 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000956 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000958 }
Guido van Rossum82598051997-03-05 00:20:32 +0000959 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000960 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000961 PyArena_Free(arena);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +0000962 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000963 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000964 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000965 return -1;
966 }
Guido van Rossum82598051997-03-05 00:20:32 +0000967 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000968 return 0;
969}
970
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000971/* Check whether a file maybe a pyc file: Look at the extension,
972 the file type, and, if we may close it, at the first few bytes. */
973
974static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000975maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000976{
977 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
978 return 1;
979
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000980 /* Only look into the file if we are allowed to close it, since
981 it then should also be seekable. */
982 if (closeit) {
983 /* Read only two bytes of the magic. If the file was opened in
984 text mode, the bytes 3 and 4 of the magic (\r\n) might not
985 be read as they are on disk. */
986 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
987 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000988 /* Mess: In case of -x, the stream is NOT at its start now,
989 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000990 which makes the current stream position formally undefined,
991 and a x-platform nightmare.
992 Unfortunately, we have no direct way to know whether -x
993 was specified. So we use a terrible hack: if the current
994 stream position is not 0, we assume -x was specified, and
995 give up. Bug 132850 on SourceForge spells out the
996 hopelessness of trying anything else (fseek and ftell
997 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000998 */
Tim Peters3e876562001-02-11 04:35:39 +0000999 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001000 if (ftell(fp) == 0) {
1001 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +00001002 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001003 ispyc = 1;
1004 rewind(fp);
1005 }
Tim Peters3e876562001-02-11 04:35:39 +00001006 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001007 }
1008 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001009}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001010
Guido van Rossum0df002c2000-08-27 19:21:52 +00001011int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001012PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001013 PyCompilerFlags *flags)
1014{
Guido van Rossum82598051997-03-05 00:20:32 +00001015 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001016 const char *ext;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001017 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001018
Guido van Rossum82598051997-03-05 00:20:32 +00001019 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001020 if (m == NULL)
1021 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001022 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +00001023 if (PyDict_GetItemString(d, "__file__") == NULL) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001024 PyObject *f;
1025 f = PyUnicode_DecodeFSDefault(filename);
Fred Drake8ed02042002-10-17 21:24:58 +00001026 if (f == NULL)
1027 return -1;
1028 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1029 Py_DECREF(f);
1030 return -1;
1031 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001032 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +00001033 Py_DECREF(f);
1034 }
Guido van Rossumfdef2711994-09-14 13:31:04 +00001035 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001036 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001037 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +00001038 if (closeit)
1039 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +00001040 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001041 fprintf(stderr, "python: Can't reopen .pyc file\n");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001042 ret = -1;
1043 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001044 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +00001045 /* Turn on optimization if a .pyo file is given */
1046 if (strcmp(ext, ".pyo") == 0)
1047 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001048 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001049 } else {
Tim Petersd08e3822003-04-17 15:24:21 +00001050 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001051 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001052 }
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +00001053 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001054 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001055 PyErr_Print();
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056 ret = -1;
1057 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001058 }
Guido van Rossum82598051997-03-05 00:20:32 +00001059 Py_DECREF(v);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001060 ret = 0;
1061 done:
1062 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1063 PyErr_Clear();
1064 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065}
1066
1067int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001068PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001069{
Guido van Rossum82598051997-03-05 00:20:32 +00001070 PyObject *m, *d, *v;
1071 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001072 if (m == NULL)
1073 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001074 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +00001075 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001076 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001077 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001078 return -1;
1079 }
Guido van Rossum82598051997-03-05 00:20:32 +00001080 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001081 return 0;
1082}
1083
Barry Warsaw035574d1997-08-29 22:07:17 +00001084static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001085parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1086 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001087{
1088 long hold;
1089 PyObject *v;
1090
1091 /* old style errors */
1092 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +00001093 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001094 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001095
1096 /* new style errors. `err' is an instance */
1097
1098 if (! (v = PyObject_GetAttrString(err, "msg")))
1099 goto finally;
1100 *message = v;
1101
1102 if (!(v = PyObject_GetAttrString(err, "filename")))
1103 goto finally;
1104 if (v == Py_None)
1105 *filename = NULL;
Kurt B. Kaiser43b15092008-01-05 04:32:22 +00001106 else if (! (*filename = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001107 goto finally;
1108
1109 Py_DECREF(v);
1110 if (!(v = PyObject_GetAttrString(err, "lineno")))
1111 goto finally;
Christian Heimes217cfd12007-12-02 14:31:20 +00001112 hold = PyLong_AsLong(v);
Barry Warsaw035574d1997-08-29 22:07:17 +00001113 Py_DECREF(v);
1114 v = NULL;
1115 if (hold < 0 && PyErr_Occurred())
1116 goto finally;
1117 *lineno = (int)hold;
1118
1119 if (!(v = PyObject_GetAttrString(err, "offset")))
1120 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001121 if (v == Py_None) {
1122 *offset = -1;
1123 Py_DECREF(v);
1124 v = NULL;
1125 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001126 hold = PyLong_AsLong(v);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001127 Py_DECREF(v);
1128 v = NULL;
1129 if (hold < 0 && PyErr_Occurred())
1130 goto finally;
1131 *offset = (int)hold;
1132 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001133
1134 if (!(v = PyObject_GetAttrString(err, "text")))
1135 goto finally;
1136 if (v == Py_None)
1137 *text = NULL;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001138 else if (!PyUnicode_Check(v) ||
1139 !(*text = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001140 goto finally;
1141 Py_DECREF(v);
1142 return 1;
1143
1144finally:
1145 Py_XDECREF(v);
1146 return 0;
1147}
1148
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001149void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001150PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001151{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001152 PyErr_PrintEx(1);
1153}
1154
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001155static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001156print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001157{
1158 char *nl;
1159 if (offset >= 0) {
1160 if (offset > 0 && offset == (int)strlen(text))
1161 offset--;
1162 for (;;) {
1163 nl = strchr(text, '\n');
1164 if (nl == NULL || nl-text >= offset)
1165 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001166 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001167 text = nl+1;
1168 }
1169 while (*text == ' ' || *text == '\t') {
1170 text++;
1171 offset--;
1172 }
1173 }
1174 PyFile_WriteString(" ", f);
1175 PyFile_WriteString(text, f);
1176 if (*text == '\0' || text[strlen(text)-1] != '\n')
1177 PyFile_WriteString("\n", f);
1178 if (offset == -1)
1179 return;
1180 PyFile_WriteString(" ", f);
1181 offset--;
1182 while (offset > 0) {
1183 PyFile_WriteString(" ", f);
1184 offset--;
1185 }
1186 PyFile_WriteString("^\n", f);
1187}
1188
Guido van Rossum66e8e862001-03-23 17:54:43 +00001189static void
1190handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001191{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001192 PyObject *exception, *value, *tb;
1193 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001194
Guido van Rossumd8faa362007-04-27 19:54:29 +00001195 if (Py_InspectFlag)
1196 /* Don't exit if -i flag was given. This flag is set to 0
1197 * when entering interactive mode for inspecting. */
1198 return;
1199
Guido van Rossum66e8e862001-03-23 17:54:43 +00001200 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001201 fflush(stdout);
1202 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001203 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001204 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001205 /* The error code should be in the `code' attribute. */
1206 PyObject *code = PyObject_GetAttrString(value, "code");
1207 if (code) {
1208 Py_DECREF(value);
1209 value = code;
1210 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001211 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001212 }
1213 /* If we failed to dig out the 'code' attribute,
1214 just let the else clause below print the error. */
1215 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001216 if (PyLong_Check(value))
1217 exitcode = (int)PyLong_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001218 else {
1219 PyObject_Print(value, stderr, Py_PRINT_RAW);
1220 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001221 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001222 }
Tim Peterscf615b52003-04-19 18:47:02 +00001223 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001224 /* Restore and clear the exception info, in order to properly decref
1225 * the exception, value, and traceback. If we just exit instead,
1226 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1227 * some finalizers from running.
1228 */
Tim Peterscf615b52003-04-19 18:47:02 +00001229 PyErr_Restore(exception, value, tb);
1230 PyErr_Clear();
1231 Py_Exit(exitcode);
1232 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001233}
1234
1235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001236PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001237{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001238 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001239
1240 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1241 handle_system_exit();
1242 }
Guido van Rossum82598051997-03-05 00:20:32 +00001243 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (exception == NULL)
1245 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001246 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001247 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001248 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001249 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001250 if (set_sys_last_vars) {
1251 PySys_SetObject("last_type", exception);
1252 PySys_SetObject("last_value", v);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001253 PySys_SetObject("last_traceback", tb ? tb : Py_None);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001254 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001255 hook = PySys_GetObject("excepthook");
1256 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001257 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001258 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001259 PyObject *result = PyEval_CallObject(hook, args);
1260 if (result == NULL) {
1261 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001262 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1263 handle_system_exit();
1264 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001265 PyErr_Fetch(&exception2, &v2, &tb2);
1266 PyErr_NormalizeException(&exception2, &v2, &tb2);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001267 /* It should not be possible for exception2 or v2
1268 to be NULL. However PyErr_Display() can't
1269 tolerate NULLs, so just be safe. */
1270 if (exception2 == NULL) {
1271 exception2 = Py_None;
1272 Py_INCREF(exception2);
1273 }
1274 if (v2 == NULL) {
1275 v2 = Py_None;
1276 Py_INCREF(v2);
1277 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001278 fflush(stdout);
1279 PySys_WriteStderr("Error in sys.excepthook:\n");
1280 PyErr_Display(exception2, v2, tb2);
1281 PySys_WriteStderr("\nOriginal exception was:\n");
1282 PyErr_Display(exception, v, tb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001283 Py_DECREF(exception2);
1284 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001285 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001286 }
1287 Py_XDECREF(result);
1288 Py_XDECREF(args);
1289 } else {
1290 PySys_WriteStderr("sys.excepthook is missing\n");
1291 PyErr_Display(exception, v, tb);
1292 }
1293 Py_XDECREF(exception);
1294 Py_XDECREF(v);
1295 Py_XDECREF(tb);
1296}
1297
Thomas Wouters477c8d52006-05-27 19:21:47 +00001298void
1299PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001300{
1301 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001302 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001303 Py_INCREF(value);
Christian Heimes2be03732007-11-15 02:26:46 +00001304 if (f == Py_None) {
1305 /* pass */
1306 }
1307 else if (f == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001308 _PyObject_Dump(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001309 fprintf(stderr, "lost sys.stderr\n");
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001310 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001311 else {
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001312 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001313 if (tb && tb != Py_None)
1314 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001315 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001316 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001317 {
Guido van Rossum82598051997-03-05 00:20:32 +00001318 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001319 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001320 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001321 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001322 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001323 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001324 else {
1325 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001326 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001327 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001328 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001329 else
Guido van Rossum82598051997-03-05 00:20:32 +00001330 PyFile_WriteString(filename, f);
1331 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001332 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001333 PyFile_WriteString(buf, f);
1334 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001335 if (text != NULL)
1336 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001337 Py_DECREF(value);
1338 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001339 /* Can't be bothered to check all those
1340 PyFile_WriteString() calls */
1341 if (PyErr_Occurred())
1342 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001343 }
1344 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001345 if (err) {
1346 /* Don't do anything else */
1347 }
Brett Cannonbf364092006-03-01 04:25:17 +00001348 else if (PyExceptionClass_Check(exception)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349 PyObject* moduleName;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001350 char* className = PyExceptionClass_Name(exception);
1351 if (className != NULL) {
1352 char *dot = strrchr(className, '.');
1353 if (dot != NULL)
1354 className = dot+1;
1355 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001356
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001357 moduleName = PyObject_GetAttrString(exception, "__module__");
Guido van Rossumc5724de2007-10-10 21:38:59 +00001358 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1359 {
1360 Py_DECREF(moduleName);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001361 err = PyFile_WriteString("<unknown>", f);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001362 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001363 else {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001364 char* modstr = PyUnicode_AsString(moduleName);
Georg Brandl1a3284e2007-12-02 09:40:06 +00001365 if (modstr && strcmp(modstr, "builtins"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001366 {
1367 err = PyFile_WriteString(modstr, f);
1368 err += PyFile_WriteString(".", f);
1369 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001371 }
1372 if (err == 0) {
1373 if (className == NULL)
1374 err = PyFile_WriteString("<unknown>", f);
1375 else
Brett Cannonbf364092006-03-01 04:25:17 +00001376 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001377 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001378 }
1379 else
1380 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001381 if (err == 0 && (value != Py_None)) {
Thomas Heller519a0422007-11-15 20:48:54 +00001382 PyObject *s = PyObject_Str(value);
Brett Cannon2e63b732006-03-02 18:34:57 +00001383 /* only print colon if the str() of the
1384 object is not the empty string
1385 */
1386 if (s == NULL)
1387 err = -1;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001388 else if (!PyUnicode_Check(s) ||
1389 PyUnicode_GetSize(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001390 err = PyFile_WriteString(": ", f);
1391 if (err == 0)
1392 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1393 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001394 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001395 /* try to write a newline in any case */
1396 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001397 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001398 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001399 /* If an error happened here, don't show it.
1400 XXX This is wrong, but too many callers rely on this behavior. */
1401 if (err != 0)
1402 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001403}
1404
Guido van Rossum82598051997-03-05 00:20:32 +00001405PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001406PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001408{
Neal Norwitze92fba02006-03-04 18:52:26 +00001409 PyObject *ret = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001410 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001411 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412 if (arena == NULL)
1413 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001414
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001415 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001416 if (mod != NULL)
1417 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001418 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001420}
1421
1422PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001423PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001424 PyObject *locals, int closeit, PyCompilerFlags *flags)
1425{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 PyObject *ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001427 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001428 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001429 if (arena == NULL)
1430 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001431
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001432 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001433 flags, NULL, arena);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001434 if (closeit)
1435 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001436 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001437 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001439 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001440 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001441 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001443}
1444
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001445static void
1446flush_io(void)
1447{
1448 PyObject *f, *r;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001449 PyObject *type, *value, *traceback;
1450
1451 /* Save the current exception */
1452 PyErr_Fetch(&type, &value, &traceback);
1453
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001454 f = PySys_GetObject("stderr");
1455 if (f != NULL) {
1456 r = PyObject_CallMethod(f, "flush", "");
1457 if (r)
1458 Py_DECREF(r);
1459 else
1460 PyErr_Clear();
1461 }
1462 f = PySys_GetObject("stdout");
1463 if (f != NULL) {
1464 r = PyObject_CallMethod(f, "flush", "");
1465 if (r)
1466 Py_DECREF(r);
1467 else
1468 PyErr_Clear();
1469 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00001470
1471 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001472}
1473
Guido van Rossum82598051997-03-05 00:20:32 +00001474static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001476 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001477{
Guido van Rossum82598051997-03-05 00:20:32 +00001478 PyCodeObject *co;
1479 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001480 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001481 if (co == NULL)
1482 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001483 v = PyEval_EvalCode(co, globals, locals);
1484 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001485 return v;
1486}
1487
Guido van Rossum82598051997-03-05 00:20:32 +00001488static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001489run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001491{
Guido van Rossum82598051997-03-05 00:20:32 +00001492 PyCodeObject *co;
1493 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001494 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001495 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001496
Guido van Rossum82598051997-03-05 00:20:32 +00001497 magic = PyMarshal_ReadLongFromFile(fp);
1498 if (magic != PyImport_GetMagicNumber()) {
1499 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001500 "Bad magic number in .pyc file");
1501 return NULL;
1502 }
Guido van Rossum82598051997-03-05 00:20:32 +00001503 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001504 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001505 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001506 if (v == NULL || !PyCode_Check(v)) {
1507 Py_XDECREF(v);
1508 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001509 "Bad code object in .pyc file");
1510 return NULL;
1511 }
Guido van Rossum82598051997-03-05 00:20:32 +00001512 co = (PyCodeObject *)v;
1513 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001514 if (v && flags)
1515 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001516 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001517 return v;
1518}
1519
Guido van Rossum82598051997-03-05 00:20:32 +00001520PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001521Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001522 PyCompilerFlags *flags)
1523{
Guido van Rossum82598051997-03-05 00:20:32 +00001524 PyCodeObject *co;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001525 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001526 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001527 if (arena == NULL)
1528 return NULL;
1529
1530 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001531 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001532 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001533 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001534 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001535 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001536 PyObject *result = PyAST_mod2obj(mod);
1537 PyArena_Free(arena);
1538 return result;
1539 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001540 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001541 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001542 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001543}
1544
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001545struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001546Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001547{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001548 struct symtable *st;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001549 mod_ty mod;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001550 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001551 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001552 if (arena == NULL)
1553 return NULL;
1554
Christian Heimesb1b3efc2008-03-26 23:24:27 +00001555 flags.cf_flags = 0;
Christian Heimes4d6ec852008-03-26 22:34:47 +00001556 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001557 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001558 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001559 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001560 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001562 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001563 return st;
1564}
1565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566/* Preferred access to parser is through AST. */
1567mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001568PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001569 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 mod_ty mod;
1572 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001573 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001574
1575 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001576 &_PyParser_Grammar, start, &err,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001577 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001579 if (flags) {
1580 flags->cf_flags |= iflags & PyCF_MASK;
1581 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001582 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 PyNode_Free(n);
1584 return mod;
1585 }
1586 else {
1587 err_input(&err);
1588 return NULL;
1589 }
1590}
1591
1592mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001593PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1594 int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001595 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001596 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 mod_ty mod;
1599 perrdetail err;
Christian Heimes3a932122008-03-26 23:25:24 +00001600 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00001601
Christian Heimes4d6ec852008-03-26 22:34:47 +00001602 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001603 &_PyParser_Grammar,
Christian Heimes4d6ec852008-03-26 22:34:47 +00001604 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 if (n) {
Christian Heimes4d6ec852008-03-26 22:34:47 +00001606 if (flags) {
1607 flags->cf_flags |= iflags & PyCF_MASK;
1608 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001609 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 PyNode_Free(n);
1611 return mod;
1612 }
1613 else {
1614 err_input(&err);
1615 if (errcode)
1616 *errcode = err.error;
1617 return NULL;
1618 }
1619}
1620
Guido van Rossuma110aa61994-08-29 12:50:44 +00001621/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001622
Guido van Rossuma110aa61994-08-29 12:50:44 +00001623node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001624PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001625{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001626 perrdetail err;
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001627 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1628 &_PyParser_Grammar,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001629 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001630 if (n == NULL)
1631 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001632
Guido van Rossuma110aa61994-08-29 12:50:44 +00001633 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001634}
1635
Guido van Rossuma110aa61994-08-29 12:50:44 +00001636/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001637
Guido van Rossuma110aa61994-08-29 12:50:44 +00001638node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001639PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001640{
Tim Petersfe2127d2001-07-16 05:37:24 +00001641 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001642 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1643 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001644 if (n == NULL)
1645 err_input(&err);
1646 return n;
1647}
1648
1649node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001650PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001651 int start, int flags)
1652{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001653 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1655 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001656 if (n == NULL)
1657 err_input(&err);
1658 return n;
1659}
1660
1661node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001662PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001663{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001664 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001665}
1666
Guido van Rossum66ebd912003-04-17 16:02:26 +00001667/* May want to move a more generalized form of this to parsetok.c or
1668 even parser modules. */
1669
1670void
1671PyParser_SetError(perrdetail *err)
1672{
1673 err_input(err);
1674}
1675
Guido van Rossuma110aa61994-08-29 12:50:44 +00001676/* Set the error appropriate to the given input error code (see errcode.h) */
1677
1678static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001679err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001680{
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001681 PyObject *v, *w, *errtype, *errtext;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001682 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001683 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001684 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001685 switch (err->error) {
1686 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001687 errtype = PyExc_IndentationError;
1688 if (err->expected == INDENT)
1689 msg = "expected an indented block";
1690 else if (err->token == INDENT)
1691 msg = "unexpected indent";
1692 else if (err->token == DEDENT)
1693 msg = "unexpected unindent";
1694 else {
1695 errtype = PyExc_SyntaxError;
1696 msg = "invalid syntax";
1697 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001698 break;
1699 case E_TOKEN:
1700 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001701 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001702 case E_EOFS:
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +00001703 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001704 break;
1705 case E_EOLS:
Alexandre Vassalotti8ae3e052008-05-16 00:41:41 +00001706 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001707 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001708 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001709 if (!PyErr_Occurred())
1710 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001711 return;
1712 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001713 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001714 return;
1715 case E_EOF:
1716 msg = "unexpected EOF while parsing";
1717 break;
Fred Drake85f36392000-07-11 17:53:00 +00001718 case E_TABSPACE:
1719 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001720 msg = "inconsistent use of tabs and spaces in indentation";
1721 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001722 case E_OVERFLOW:
1723 msg = "expression too long";
1724 break;
Fred Drake85f36392000-07-11 17:53:00 +00001725 case E_DEDENT:
1726 errtype = PyExc_IndentationError;
1727 msg = "unindent does not match any outer indentation level";
1728 break;
1729 case E_TOODEEP:
1730 errtype = PyExc_IndentationError;
1731 msg = "too many levels of indentation";
1732 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001733 case E_DECODE: {
1734 PyObject *type, *value, *tb;
1735 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001736 if (value != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +00001737 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001738 if (u != NULL) {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001739 msg = PyUnicode_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001740 }
1741 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001742 if (msg == NULL)
1743 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001744 Py_XDECREF(type);
1745 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001746 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001747 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001748 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001749 case E_LINECONT:
1750 msg = "unexpected character after line continuation character";
1751 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001752
1753 case E_IDENTIFIER:
1754 msg = "invalid character in identifier";
1755 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001756 default:
1757 fprintf(stderr, "error=%d\n", err->error);
1758 msg = "unknown parsing error";
1759 break;
1760 }
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001761 /* err->text may not be UTF-8 in case of decoding errors.
1762 Explicitly convert to an object. */
1763 if (!err->text) {
1764 errtext = Py_None;
1765 Py_INCREF(Py_None);
1766 } else {
1767 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1768 "replace");
1769 }
1770 v = Py_BuildValue("(ziiN)", err->filename,
1771 err->lineno, err->offset, errtext);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001772 if (err->text != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001773 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 err->text = NULL;
1775 }
1776 w = NULL;
1777 if (v != NULL)
1778 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001779 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001780 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001781 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001782 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001783}
1784
1785/* Print fatal error message and abort */
1786
1787void
Tim Peters7c321a82002-07-09 02:57:01 +00001788Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001789{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001790 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001791 if (PyErr_Occurred()) {
1792 PyErr_Print();
1793 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001794#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001795 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001796 OutputDebugString(msg);
1797 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001798#ifdef _DEBUG
1799 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001800#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001801#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001802 abort();
1803}
1804
1805/* Clean up and exit */
1806
Guido van Rossuma110aa61994-08-29 12:50:44 +00001807#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001808#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001809#endif
1810
Collin Winter670e6922007-03-21 02:57:17 +00001811static void (*pyexitfunc)(void) = NULL;
1812/* For the atexit module. */
1813void _Py_PyAtExit(void (*func)(void))
1814{
1815 pyexitfunc = func;
1816}
1817
1818static void
1819call_py_exitfuncs(void)
1820{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001821 if (pyexitfunc == NULL)
Collin Winter670e6922007-03-21 02:57:17 +00001822 return;
1823
1824 (*pyexitfunc)();
1825 PyErr_Clear();
1826}
1827
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001828#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001829static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001830static int nexitfuncs = 0;
1831
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001832int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001833{
1834 if (nexitfuncs >= NEXITFUNCS)
1835 return -1;
1836 exitfuncs[nexitfuncs++] = func;
1837 return 0;
1838}
1839
Guido van Rossumcc283f51997-08-05 02:22:03 +00001840static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001841call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001842{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001843 while (nexitfuncs > 0)
1844 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001845
1846 fflush(stdout);
1847 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001848}
1849
1850void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001851Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001852{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001853 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001854
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001855 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001856}
1857
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001858static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001859initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001860{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001861#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001862 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001863#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001864#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001865 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001866#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001867#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001868 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001869#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001870 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001871}
1872
Guido van Rossum7433b121997-02-14 19:45:36 +00001873
1874/*
1875 * The file descriptor fd is considered ``interactive'' if either
1876 * a) isatty(fd) is TRUE, or
1877 * b) the -i flag was given, and the filename associated with
1878 * the descriptor is NULL or "<stdin>" or "???".
1879 */
1880int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001881Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001882{
1883 if (isatty((int)fileno(fp)))
1884 return 1;
1885 if (!Py_InteractiveFlag)
1886 return 0;
1887 return (filename == NULL) ||
1888 (strcmp(filename, "<stdin>") == 0) ||
1889 (strcmp(filename, "???") == 0);
1890}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001891
1892
Tim Petersd08e3822003-04-17 15:24:21 +00001893#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001894#if defined(WIN32) && defined(_MSC_VER)
1895
1896/* Stack checking for Microsoft C */
1897
1898#include <malloc.h>
1899#include <excpt.h>
1900
Fred Drakee8de31c2000-08-31 05:38:39 +00001901/*
1902 * Return non-zero when we run out of memory on the stack; zero otherwise.
1903 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001904int
Fred Drake399739f2000-08-31 05:52:44 +00001905PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001906{
1907 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001908 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001909 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001910 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001911 return 0;
Christian Heimes7131fd92008-02-19 14:21:46 +00001912 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
Trent Nelson39e307e2008-03-19 06:45:48 +00001913 EXCEPTION_EXECUTE_HANDLER :
1914 EXCEPTION_CONTINUE_SEARCH) {
Christian Heimes7131fd92008-02-19 14:21:46 +00001915 int errcode = _resetstkoflw();
1916 if (errcode)
1917 {
1918 Py_FatalError("Could not reset the stack!");
1919 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001920 }
1921 return 1;
1922}
1923
1924#endif /* WIN32 && _MSC_VER */
1925
1926/* Alternate implementations can be added here... */
1927
1928#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001929
1930
1931/* Wrappers around sigaction() or signal(). */
1932
1933PyOS_sighandler_t
1934PyOS_getsig(int sig)
1935{
1936#ifdef HAVE_SIGACTION
1937 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001938 if (sigaction(sig, NULL, &context) == -1)
1939 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001940 return context.sa_handler;
1941#else
1942 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001943/* Special signal handling for the secure CRT in Visual Studio 2005 */
1944#if defined(_MSC_VER) && _MSC_VER >= 1400
1945 switch (sig) {
1946 /* Only these signals are valid */
1947 case SIGINT:
1948 case SIGILL:
1949 case SIGFPE:
1950 case SIGSEGV:
1951 case SIGTERM:
1952 case SIGBREAK:
1953 case SIGABRT:
1954 break;
1955 /* Don't call signal() with other values or it will assert */
1956 default:
1957 return SIG_ERR;
1958 }
1959#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001960 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001961 if (handler != SIG_ERR)
1962 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001963 return handler;
1964#endif
1965}
1966
1967PyOS_sighandler_t
1968PyOS_setsig(int sig, PyOS_sighandler_t handler)
1969{
1970#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001971 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001972 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001973 sigemptyset(&context.sa_mask);
1974 context.sa_flags = 0;
1975 if (sigaction(sig, &context, &ocontext) == -1)
1976 return SIG_ERR;
1977 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001978#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001979 PyOS_sighandler_t oldhandler;
1980 oldhandler = signal(sig, handler);
1981#ifdef HAVE_SIGINTERRUPT
1982 siginterrupt(sig, 1);
1983#endif
1984 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001985#endif
1986}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987
1988/* Deprecated C API functions still provided for binary compatiblity */
1989
1990#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001991PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1993{
1994 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1995}
1996
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001997#undef PyParser_SimpleParseString
1998PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999PyParser_SimpleParseString(const char *str, int start)
2000{
2001 return PyParser_SimpleParseStringFlags(str, start, 0);
2002}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002003
2004#undef PyRun_AnyFile
2005PyAPI_FUNC(int)
2006PyRun_AnyFile(FILE *fp, const char *name)
2007{
2008 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
2009}
2010
2011#undef PyRun_AnyFileEx
2012PyAPI_FUNC(int)
2013PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2014{
2015 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
2016}
2017
2018#undef PyRun_AnyFileFlags
2019PyAPI_FUNC(int)
2020PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2021{
2022 return PyRun_AnyFileExFlags(fp, name, 0, flags);
2023}
2024
2025#undef PyRun_File
2026PyAPI_FUNC(PyObject *)
2027PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2028{
2029 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2030}
2031
2032#undef PyRun_FileEx
2033PyAPI_FUNC(PyObject *)
2034PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2035{
2036 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2037}
2038
2039#undef PyRun_FileFlags
2040PyAPI_FUNC(PyObject *)
2041PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2042 PyCompilerFlags *flags)
2043{
2044 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2045}
2046
2047#undef PyRun_SimpleFile
2048PyAPI_FUNC(int)
2049PyRun_SimpleFile(FILE *f, const char *p)
2050{
2051 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2052}
2053
2054#undef PyRun_SimpleFileEx
2055PyAPI_FUNC(int)
2056PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2057{
2058 return PyRun_SimpleFileExFlags(f, p, c, NULL);
2059}
2060
2061
2062#undef PyRun_String
2063PyAPI_FUNC(PyObject *)
2064PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2065{
2066 return PyRun_StringFlags(str, s, g, l, NULL);
2067}
2068
2069#undef PyRun_SimpleString
2070PyAPI_FUNC(int)
2071PyRun_SimpleString(const char *s)
2072{
2073 return PyRun_SimpleStringFlags(s, NULL);
2074}
2075
2076#undef Py_CompileString
2077PyAPI_FUNC(PyObject *)
2078Py_CompileString(const char *str, const char *p, int s)
2079{
2080 return Py_CompileStringFlags(str, p, s, NULL);
2081}
2082
2083#undef PyRun_InteractiveOne
2084PyAPI_FUNC(int)
2085PyRun_InteractiveOne(FILE *f, const char *p)
2086{
2087 return PyRun_InteractiveOneFlags(f, p, NULL);
2088}
2089
2090#undef PyRun_InteractiveLoop
2091PyAPI_FUNC(int)
2092PyRun_InteractiveLoop(FILE *f, const char *p)
2093{
2094 return PyRun_InteractiveLoopFlags(f, p, NULL);
2095}
2096
2097#ifdef __cplusplus
2098}
2099#endif