blob: a9b7f81ff746e7d84402533a0c5eb5561680cc55 [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"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +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"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000023#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000024
Martin v. Löwis73d538b2003-03-05 15:13:47 +000025#ifdef HAVE_LANGINFO_H
26#include <locale.h>
27#include <langinfo.h>
28#endif
29
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000030#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000031#undef BYTE
32#include "windows.h"
33#endif
34
Neal Norwitz4281cef2006-03-04 19:58:13 +000035#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000036#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000038#define PRINT_TOTAL_REFS() fprintf(stderr, \
39 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000040 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000041#endif
42
Anthony Baxterac6bd462006-04-13 02:06:09 +000043#ifdef __cplusplus
44extern "C" {
45#endif
46
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048
Guido van Rossum82598051997-03-05 00:20:32 +000049extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossumb73cc041993-11-01 16:28:59 +000051/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000052static void initmain(void);
53static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000056static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000057 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000058static void err_input(perrdetail *);
59static void initsigs(void);
60static void call_sys_exitfunc(void);
61static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000062extern void _PyUnicode_Init(void);
63extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000064
Mark Hammond8d98d2c2003-04-19 15:41:53 +000065#ifdef WITH_THREAD
66extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
67extern void _PyGILState_Fini(void);
68#endif /* WITH_THREAD */
69
Guido van Rossum82598051997-03-05 00:20:32 +000070int Py_DebugFlag; /* Needed by parser.c */
71int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000072int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000073int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000074int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000075int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000076int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000077int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000078int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000079int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000080int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000081/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
83 true divisions (which they will be in 2.3). */
84int _Py_QnewFlag = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000085
Brett Cannone9746892008-04-12 23:44:07 +000086/* PyModule_GetWarningsModule is no longer necessary as of 2.6
87since _warnings is builtin. This API should not be used. */
88PyObject *
89PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000090{
Brett Cannone9746892008-04-12 23:44:07 +000091 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000092}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000093
Guido van Rossum25ce5661997-08-02 03:10:38 +000094static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000095
Thomas Wouters7e474022000-07-16 12:04:32 +000096/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000097
98int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000099Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000100{
101 return initialized;
102}
103
Guido van Rossum25ce5661997-08-02 03:10:38 +0000104/* Global initializations. Can be undone by Py_Finalize(). Don't
105 call this twice without an intervening Py_Finalize() call. When
106 initializations fail, a fatal error is issued and the function does
107 not return. On return, the first thread and interpreter state have
108 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000109
Guido van Rossum25ce5661997-08-02 03:10:38 +0000110 Locking: you must hold the interpreter lock while calling this.
111 (If the lock has not yet been initialized, that's equivalent to
112 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000113
Guido van Rossum25ce5661997-08-02 03:10:38 +0000114*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000115
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000116static int
117add_flag(int flag, const char *envs)
118{
119 int env = atoi(envs);
120 if (flag < env)
121 flag = env;
122 if (flag < 1)
123 flag = 1;
124 return flag;
125}
126
Guido van Rossuma027efa1997-05-05 20:56:21 +0000127void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000128Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000129{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000131 PyThreadState *tstate;
132 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000133 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000134#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
135 char *codeset;
136 char *saved_locale;
137 PyObject *sys_stream, *sys_isatty;
138#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
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000145 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000146 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000147 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000148 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000149 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000150 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000151 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
152 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000153
Guido van Rossuma027efa1997-05-05 20:56:21 +0000154 interp = PyInterpreterState_New();
155 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000156 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000157
Guido van Rossuma027efa1997-05-05 20:56:21 +0000158 tstate = PyThreadState_New(interp);
159 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000160 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000161 (void) PyThreadState_Swap(tstate);
162
Guido van Rossum70d893a2001-08-16 08:21:42 +0000163 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000164
Neal Norwitzb2501f42002-12-31 03:42:13 +0000165 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000166 Py_FatalError("Py_Initialize: can't init frames");
167
Neal Norwitzb2501f42002-12-31 03:42:13 +0000168 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000169 Py_FatalError("Py_Initialize: can't init ints");
170
Christian Heimes1a6387e2008-03-26 12:49:49 +0000171 if (!PyBytes_Init())
172 Py_FatalError("Py_Initialize: can't init bytearray");
173
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000174 _PyFloat_Init();
175
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176 interp->modules = PyDict_New();
177 if (interp->modules == NULL)
178 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000179 interp->modules_reloading = PyDict_New();
180 if (interp->modules_reloading == NULL)
181 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000182
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000183#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000184 /* Init Unicode implementation; relies on the codec registry */
185 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000186#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000187
Barry Warsawf242aa02000-05-25 23:09:49 +0000188 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000189 if (bimod == NULL)
190 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000191 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000192 if (interp->builtins == NULL)
193 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000194 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195
196 sysmod = _PySys_Init();
197 if (sysmod == NULL)
198 Py_FatalError("Py_Initialize: can't initialize sys");
199 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000200 if (interp->sysdict == NULL)
201 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 Py_INCREF(interp->sysdict);
203 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000204 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 PyDict_SetItemString(interp->sysdict, "modules",
206 interp->modules);
207
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000208 _PyImport_Init();
209
Barry Warsawf242aa02000-05-25 23:09:49 +0000210 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000211 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000212 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000213
Barry Warsaw035574d1997-08-29 22:07:17 +0000214 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000215 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000216
Just van Rossum52e14d62002-12-30 22:08:05 +0000217 _PyImportHooks_Init();
218
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000219 if (install_sigs)
220 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000221
222 /* Initialize warnings. */
223 _PyWarnings_Init();
224 if (PySys_HasWarnOptions()) {
225 PyObject *warnings_module = PyImport_ImportModule("warnings");
226 if (!warnings_module)
227 PyErr_Clear();
228 Py_XDECREF(warnings_module);
229 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000230
231 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000232 if (!Py_NoSiteFlag)
233 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000234
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000235 /* auto-thread-state API, if available */
236#ifdef WITH_THREAD
237 _PyGILState_Init(interp, tstate);
238#endif /* WITH_THREAD */
239
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000240#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
241 /* On Unix, set the file system encoding according to the
242 user's preference, if the CODESET names a well-known
243 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000244 initialized by other means. Also set the encoding of
245 stdin and stdout if these are terminals. */
246
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000247 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000248 setlocale(LC_CTYPE, "");
249 codeset = nl_langinfo(CODESET);
250 if (codeset && *codeset) {
251 PyObject *enc = PyCodec_Encoder(codeset);
252 if (enc) {
253 codeset = strdup(codeset);
254 Py_DECREF(enc);
255 } else {
256 codeset = NULL;
257 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000258 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000259 } else
260 codeset = NULL;
261 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000262 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000263
264 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000265 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000266 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
267 if (!sys_isatty)
268 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000269 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
270 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271 if (!PyFile_SetEncoding(sys_stream, codeset))
272 Py_FatalError("Cannot set codeset of stdin");
273 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274 Py_XDECREF(sys_isatty);
275
276 sys_stream = PySys_GetObject("stdout");
277 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
278 if (!sys_isatty)
279 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000280 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
281 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000282 if (!PyFile_SetEncoding(sys_stream, codeset))
283 Py_FatalError("Cannot set codeset of stdout");
284 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000285 Py_XDECREF(sys_isatty);
286
Martin v. Löwisea62d252006-04-03 10:56:49 +0000287 sys_stream = PySys_GetObject("stderr");
288 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
289 if (!sys_isatty)
290 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000291 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
292 PyFile_Check(sys_stream)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +0000293 if (!PyFile_SetEncoding(sys_stream, codeset))
294 Py_FatalError("Cannot set codeset of stderr");
295 }
296 Py_XDECREF(sys_isatty);
297
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000298 if (!Py_FileSystemDefaultEncoding)
299 Py_FileSystemDefaultEncoding = codeset;
300 else
301 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000302 }
303#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000304}
305
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000306void
307Py_Initialize(void)
308{
309 Py_InitializeEx(1);
310}
311
312
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000313#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000314extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000315#endif
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 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000349 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000350 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000351
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000352 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000353 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000354 interp = tstate->interp;
355
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000356 /* Disable signal handling */
357 PyOS_FiniInterrupts();
358
Christian Heimes908caac2008-01-27 23:34:59 +0000359 /* Clear type lookup cache */
360 PyType_ClearCache();
361
Guido van Rossume13ddc92003-04-17 17:29:22 +0000362 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000363 * before all modules are destroyed.
364 * XXX If a __del__ or weakref callback is triggered here, and tries to
365 * XXX import a module, bad things can happen, because Python no
366 * XXX longer believes it's initialized.
367 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
368 * XXX is easy to provoke that way. I've also seen, e.g.,
369 * XXX Exception exceptions.ImportError: 'No module named sha'
370 * XXX in <function callback at 0x008F5718> ignored
371 * XXX but I'm unclear on exactly how that one happens. In any case,
372 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000373 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000374 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000375#ifdef COUNT_ALLOCS
376 /* With COUNT_ALLOCS, it helps to run GC multiple times:
377 each collection might release some types from the type
378 list, so they become garbage. */
379 while (PyGC_Collect() > 0)
380 /* nothing */;
381#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000382
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000383 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000384 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000385
Guido van Rossume13ddc92003-04-17 17:29:22 +0000386 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000387 * new-style class definitions, for example.
388 * XXX This is disabled because it caused too many problems. If
389 * XXX a __del__ or weakref callback triggers here, Python code has
390 * XXX a hard time running, because even the sys module has been
391 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
392 * XXX One symptom is a sequence of information-free messages
393 * XXX coming from threads (if a __del__ or callback is invoked,
394 * XXX other threads can execute too, and any exception they encounter
395 * XXX triggers a comedy of errors as subsystem after subsystem
396 * XXX fails to find what it *expects* to find in sys to help report
397 * XXX the exception and consequent unexpected failures). I've also
398 * XXX seen segfaults then, after adding print statements to the
399 * XXX Python code getting called.
400 */
401#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000402 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000403#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000404
Guido van Rossum1707aad1997-12-08 23:43:45 +0000405 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
406 _PyImport_Fini();
407
408 /* Debugging stuff */
409#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000410 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000411#endif
412
Tim Peters62e97f02006-03-28 21:44:32 +0000413 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000414
Tim Peters9cf25ce2003-04-17 15:21:01 +0000415#ifdef Py_TRACE_REFS
416 /* Display all objects still alive -- this can invoke arbitrary
417 * __repr__ overrides, so requires a mostly-intact interpreter.
418 * Alas, a lot of stuff may still be alive now that will be cleaned
419 * up later.
420 */
Tim Peters269b2a62003-04-17 19:52:29 +0000421 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000422 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000423#endif /* Py_TRACE_REFS */
424
Guido van Rossumd922fa42003-04-15 14:10:09 +0000425 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000426 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000427
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000428 /* Now we decref the exception classes. After this point nothing
429 can raise an exception. That's okay, because each Fini() method
430 below has been checked to make sure no exceptions are ever
431 raised.
432 */
433
434 _PyExc_Fini();
435
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000436 /* Cleanup auto-thread-state */
437#ifdef WITH_THREAD
438 _PyGILState_Fini();
439#endif /* WITH_THREAD */
440
Guido van Rossumd922fa42003-04-15 14:10:09 +0000441 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000442 PyThreadState_Swap(NULL);
443 PyInterpreterState_Delete(interp);
444
Guido van Rossumd922fa42003-04-15 14:10:09 +0000445 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000446 PyMethod_Fini();
447 PyFrame_Fini();
448 PyCFunction_Fini();
449 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000450 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000451 PySet_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000452 PyString_Fini();
Christian Heimes1a6387e2008-03-26 12:49:49 +0000453 PyBytes_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000454 PyInt_Fini();
455 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000456 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000457
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000458#ifdef Py_USING_UNICODE
459 /* Cleanup Unicode implementation */
460 _PyUnicode_Fini();
461#endif
462
Guido van Rossumcc283f51997-08-05 02:22:03 +0000463 /* XXX Still allocated:
464 - various static ad-hoc pointers to interned strings
465 - int and float free list blocks
466 - whatever various modules and libraries allocate
467 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000468
469 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000470
Tim Peters269b2a62003-04-17 19:52:29 +0000471#ifdef Py_TRACE_REFS
472 /* Display addresses (& refcnts) of all objects still alive.
473 * An address can be used to find the repr of the object, printed
474 * above by _Py_PrintReferences.
475 */
476 if (Py_GETENV("PYTHONDUMPREFS"))
477 _Py_PrintReferenceAddresses(stderr);
478#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000479#ifdef PYMALLOC_DEBUG
480 if (Py_GETENV("PYTHONMALLOCSTATS"))
481 _PyObject_DebugMallocStats();
482#endif
483
Guido van Rossumcc283f51997-08-05 02:22:03 +0000484 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000485}
486
487/* Create and initialize a new interpreter and thread, and return the
488 new thread. This requires that Py_Initialize() has been called
489 first.
490
491 Unsuccessful initialization yields a NULL pointer. Note that *no*
492 exception information is available even in this case -- the
493 exception information is held in the thread, and there is no
494 thread.
495
496 Locking: as above.
497
498*/
499
500PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000501Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000502{
503 PyInterpreterState *interp;
504 PyThreadState *tstate, *save_tstate;
505 PyObject *bimod, *sysmod;
506
507 if (!initialized)
508 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
509
510 interp = PyInterpreterState_New();
511 if (interp == NULL)
512 return NULL;
513
514 tstate = PyThreadState_New(interp);
515 if (tstate == NULL) {
516 PyInterpreterState_Delete(interp);
517 return NULL;
518 }
519
520 save_tstate = PyThreadState_Swap(tstate);
521
522 /* XXX The following is lax in error checking */
523
524 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000525 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000526
527 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
528 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000529 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000530 if (interp->builtins == NULL)
531 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000532 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533 }
534 sysmod = _PyImport_FindExtension("sys", "sys");
535 if (bimod != NULL && sysmod != NULL) {
536 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000537 if (interp->sysdict == NULL)
538 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000539 Py_INCREF(interp->sysdict);
540 PySys_SetPath(Py_GetPath());
541 PyDict_SetItemString(interp->sysdict, "modules",
542 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000543 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000545 if (!Py_NoSiteFlag)
546 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547 }
548
549 if (!PyErr_Occurred())
550 return tstate;
551
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000552handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000553 /* Oops, it didn't work. Undo it all. */
554
555 PyErr_Print();
556 PyThreadState_Clear(tstate);
557 PyThreadState_Swap(save_tstate);
558 PyThreadState_Delete(tstate);
559 PyInterpreterState_Delete(interp);
560
561 return NULL;
562}
563
564/* Delete an interpreter and its last thread. This requires that the
565 given thread state is current, that the thread has no remaining
566 frames, and that it is its interpreter's only remaining thread.
567 It is a fatal error to violate these constraints.
568
569 (Py_Finalize() doesn't have these constraints -- it zaps
570 everything, regardless.)
571
572 Locking: as above.
573
574*/
575
576void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000577Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578{
579 PyInterpreterState *interp = tstate->interp;
580
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000581 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000582 Py_FatalError("Py_EndInterpreter: thread is not current");
583 if (tstate->frame != NULL)
584 Py_FatalError("Py_EndInterpreter: thread still has a frame");
585 if (tstate != interp->tstate_head || tstate->next != NULL)
586 Py_FatalError("Py_EndInterpreter: not the last thread");
587
588 PyImport_Cleanup();
589 PyInterpreterState_Clear(interp);
590 PyThreadState_Swap(NULL);
591 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000592}
593
594static char *progname = "python";
595
596void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000597Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000598{
599 if (pn && *pn)
600 progname = pn;
601}
602
603char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000604Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000605{
606 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000607}
608
Guido van Rossuma61691e1998-02-06 22:27:24 +0000609static char *default_home = NULL;
610
611void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000612Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000613{
614 default_home = home;
615}
616
617char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000618Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000619{
620 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000621 if (home == NULL && !Py_IgnoreEnvironmentFlag)
622 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000623 return home;
624}
625
Guido van Rossum6135a871995-01-09 17:53:26 +0000626/* Create __main__ module */
627
628static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000629initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000630{
Guido van Rossum82598051997-03-05 00:20:32 +0000631 PyObject *m, *d;
632 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000633 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000634 Py_FatalError("can't create __main__ module");
635 d = PyModule_GetDict(m);
636 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000637 PyObject *bimod = PyImport_ImportModule("__builtin__");
638 if (bimod == NULL ||
639 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000640 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000641 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000642 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000643}
644
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000645/* Import the site module (not into __main__ though) */
646
647static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000648initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000649{
650 PyObject *m, *f;
651 m = PyImport_ImportModule("site");
652 if (m == NULL) {
653 f = PySys_GetObject("stderr");
654 if (Py_VerboseFlag) {
655 PyFile_WriteString(
656 "'import site' failed; traceback:\n", f);
657 PyErr_Print();
658 }
659 else {
660 PyFile_WriteString(
661 "'import site' failed; use -v for traceback\n", f);
662 PyErr_Clear();
663 }
664 }
665 else {
666 Py_DECREF(m);
667 }
668}
669
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000670/* Parse input from a file and execute it */
671
672int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000673PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000674 PyCompilerFlags *flags)
675{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000676 if (filename == NULL)
677 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000678 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000679 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000680 if (closeit)
681 fclose(fp);
682 return err;
683 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000684 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000685 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000686}
687
688int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000689PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000690{
Guido van Rossum82598051997-03-05 00:20:32 +0000691 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000692 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000693 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000694
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000695 if (flags == NULL) {
696 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000697 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000698 }
Guido van Rossum82598051997-03-05 00:20:32 +0000699 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000700 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000701 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
702 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000703 }
Guido van Rossum82598051997-03-05 00:20:32 +0000704 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000705 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000706 PySys_SetObject("ps2", v = PyString_FromString("... "));
707 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000708 }
709 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000710 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000711 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000712 if (ret == E_EOF)
713 return 0;
714 /*
715 if (ret == E_NOMEM)
716 return -1;
717 */
718 }
719}
720
Eric Smith7c478942008-03-18 23:45:49 +0000721#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000722/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000723#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000724 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000725 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000726#endif
727#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000728/* Keep an example of flags with future keyword support. */
729#define PARSER_FLAGS(flags) \
730 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000731 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000732 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
733 PyPARSE_PRINT_IS_FUNCTION : 0) \
734 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
735 PyPARSE_UNICODE_LITERALS : 0) \
736 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000737#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000738
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000739int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000740PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000741{
Guido van Rossum82598051997-03-05 00:20:32 +0000742 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000744 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000745 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000747
Guido van Rossum82598051997-03-05 00:20:32 +0000748 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000749 if (v != NULL) {
750 v = PyObject_Str(v);
751 if (v == NULL)
752 PyErr_Clear();
753 else if (PyString_Check(v))
754 ps1 = PyString_AsString(v);
755 }
Guido van Rossum82598051997-03-05 00:20:32 +0000756 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000757 if (w != NULL) {
758 w = PyObject_Str(w);
759 if (w == NULL)
760 PyErr_Clear();
761 else if (PyString_Check(w))
762 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000763 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000764 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000765 if (arena == NULL) {
766 Py_XDECREF(v);
767 Py_XDECREF(w);
768 return -1;
769 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000770 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000772 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000773 Py_XDECREF(v);
774 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000776 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 if (errcode == E_EOF) {
778 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000779 return E_EOF;
780 }
Guido van Rossum82598051997-03-05 00:20:32 +0000781 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000783 }
Guido van Rossum82598051997-03-05 00:20:32 +0000784 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000785 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000786 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000787 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000788 }
Guido van Rossum82598051997-03-05 00:20:32 +0000789 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000790 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000791 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000792 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000793 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000794 return -1;
795 }
Guido van Rossum82598051997-03-05 00:20:32 +0000796 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000797 if (Py_FlushLine())
798 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000799 return 0;
800}
801
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000802/* Check whether a file maybe a pyc file: Look at the extension,
803 the file type, and, if we may close it, at the first few bytes. */
804
805static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000806maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000807{
808 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
809 return 1;
810
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000811 /* Only look into the file if we are allowed to close it, since
812 it then should also be seekable. */
813 if (closeit) {
814 /* Read only two bytes of the magic. If the file was opened in
815 text mode, the bytes 3 and 4 of the magic (\r\n) might not
816 be read as they are on disk. */
817 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
818 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000819 /* Mess: In case of -x, the stream is NOT at its start now,
820 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000821 which makes the current stream position formally undefined,
822 and a x-platform nightmare.
823 Unfortunately, we have no direct way to know whether -x
824 was specified. So we use a terrible hack: if the current
825 stream position is not 0, we assume -x was specified, and
826 give up. Bug 132850 on SourceForge spells out the
827 hopelessness of trying anything else (fseek and ftell
828 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000829 */
Tim Peters3e876562001-02-11 04:35:39 +0000830 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000831 if (ftell(fp) == 0) {
832 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000833 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000834 ispyc = 1;
835 rewind(fp);
836 }
Tim Peters3e876562001-02-11 04:35:39 +0000837 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000838 }
839 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000840}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000841
Guido van Rossum0df002c2000-08-27 19:21:52 +0000842int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000843PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000844 PyCompilerFlags *flags)
845{
Guido van Rossum82598051997-03-05 00:20:32 +0000846 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000847 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000848 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000849
Guido van Rossum82598051997-03-05 00:20:32 +0000850 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000851 if (m == NULL)
852 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000853 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000854 if (PyDict_GetItemString(d, "__file__") == NULL) {
855 PyObject *f = PyString_FromString(filename);
856 if (f == NULL)
857 return -1;
858 if (PyDict_SetItemString(d, "__file__", f) < 0) {
859 Py_DECREF(f);
860 return -1;
861 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000862 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000863 Py_DECREF(f);
864 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000865 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000866 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000867 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000868 if (closeit)
869 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000870 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000871 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000872 ret = -1;
873 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000874 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000875 /* Turn on optimization if a .pyo file is given */
876 if (strcmp(ext, ".pyo") == 0)
877 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000878 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000879 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000880 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000881 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000882 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000883 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000884 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000885 ret = -1;
886 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000887 }
Guido van Rossum82598051997-03-05 00:20:32 +0000888 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000889 if (Py_FlushLine())
890 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000891 ret = 0;
892 done:
893 if (set_file_name && PyDict_DelItemString(d, "__file__"))
894 PyErr_Clear();
895 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000896}
897
898int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000899PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000900{
Guido van Rossum82598051997-03-05 00:20:32 +0000901 PyObject *m, *d, *v;
902 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000903 if (m == NULL)
904 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000905 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000906 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000907 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000908 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000909 return -1;
910 }
Guido van Rossum82598051997-03-05 00:20:32 +0000911 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000912 if (Py_FlushLine())
913 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000914 return 0;
915}
916
Barry Warsaw035574d1997-08-29 22:07:17 +0000917static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000918parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
919 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000920{
921 long hold;
922 PyObject *v;
923
924 /* old style errors */
925 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000926 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000927 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000928
929 /* new style errors. `err' is an instance */
930
931 if (! (v = PyObject_GetAttrString(err, "msg")))
932 goto finally;
933 *message = v;
934
935 if (!(v = PyObject_GetAttrString(err, "filename")))
936 goto finally;
937 if (v == Py_None)
938 *filename = NULL;
939 else if (! (*filename = PyString_AsString(v)))
940 goto finally;
941
942 Py_DECREF(v);
943 if (!(v = PyObject_GetAttrString(err, "lineno")))
944 goto finally;
945 hold = PyInt_AsLong(v);
946 Py_DECREF(v);
947 v = NULL;
948 if (hold < 0 && PyErr_Occurred())
949 goto finally;
950 *lineno = (int)hold;
951
952 if (!(v = PyObject_GetAttrString(err, "offset")))
953 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000954 if (v == Py_None) {
955 *offset = -1;
956 Py_DECREF(v);
957 v = NULL;
958 } else {
959 hold = PyInt_AsLong(v);
960 Py_DECREF(v);
961 v = NULL;
962 if (hold < 0 && PyErr_Occurred())
963 goto finally;
964 *offset = (int)hold;
965 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000966
967 if (!(v = PyObject_GetAttrString(err, "text")))
968 goto finally;
969 if (v == Py_None)
970 *text = NULL;
971 else if (! (*text = PyString_AsString(v)))
972 goto finally;
973 Py_DECREF(v);
974 return 1;
975
976finally:
977 Py_XDECREF(v);
978 return 0;
979}
980
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000981void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000982PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000983{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000984 PyErr_PrintEx(1);
985}
986
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000987static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000988print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000989{
990 char *nl;
991 if (offset >= 0) {
992 if (offset > 0 && offset == (int)strlen(text))
993 offset--;
994 for (;;) {
995 nl = strchr(text, '\n');
996 if (nl == NULL || nl-text >= offset)
997 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000998 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000999 text = nl+1;
1000 }
1001 while (*text == ' ' || *text == '\t') {
1002 text++;
1003 offset--;
1004 }
1005 }
1006 PyFile_WriteString(" ", f);
1007 PyFile_WriteString(text, f);
1008 if (*text == '\0' || text[strlen(text)-1] != '\n')
1009 PyFile_WriteString("\n", f);
1010 if (offset == -1)
1011 return;
1012 PyFile_WriteString(" ", f);
1013 offset--;
1014 while (offset > 0) {
1015 PyFile_WriteString(" ", f);
1016 offset--;
1017 }
1018 PyFile_WriteString("^\n", f);
1019}
1020
Guido van Rossum66e8e862001-03-23 17:54:43 +00001021static void
1022handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001023{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001024 PyObject *exception, *value, *tb;
1025 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001026
Georg Brandl49aafc92007-03-07 00:34:46 +00001027 if (Py_InspectFlag)
1028 /* Don't exit if -i flag was given. This flag is set to 0
1029 * when entering interactive mode for inspecting. */
1030 return;
1031
Guido van Rossum66e8e862001-03-23 17:54:43 +00001032 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001033 if (Py_FlushLine())
1034 PyErr_Clear();
1035 fflush(stdout);
1036 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001037 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001038 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001039 /* The error code should be in the `code' attribute. */
1040 PyObject *code = PyObject_GetAttrString(value, "code");
1041 if (code) {
1042 Py_DECREF(value);
1043 value = code;
1044 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001045 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001046 }
1047 /* If we failed to dig out the 'code' attribute,
1048 just let the else clause below print the error. */
1049 }
1050 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001051 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001052 else {
1053 PyObject_Print(value, stderr, Py_PRINT_RAW);
1054 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001055 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001056 }
Tim Peterscf615b52003-04-19 18:47:02 +00001057 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001058 /* Restore and clear the exception info, in order to properly decref
1059 * the exception, value, and traceback. If we just exit instead,
1060 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1061 * some finalizers from running.
1062 */
Tim Peterscf615b52003-04-19 18:47:02 +00001063 PyErr_Restore(exception, value, tb);
1064 PyErr_Clear();
1065 Py_Exit(exitcode);
1066 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001067}
1068
1069void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001070PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001071{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001072 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001073
1074 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1075 handle_system_exit();
1076 }
Guido van Rossum82598051997-03-05 00:20:32 +00001077 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 if (exception == NULL)
1079 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001080 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001081 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001082 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001083 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001084 if (set_sys_last_vars) {
1085 PySys_SetObject("last_type", exception);
1086 PySys_SetObject("last_value", v);
1087 PySys_SetObject("last_traceback", tb);
1088 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001089 hook = PySys_GetObject("excepthook");
1090 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001091 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001092 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001093 PyObject *result = PyEval_CallObject(hook, args);
1094 if (result == NULL) {
1095 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001096 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1097 handle_system_exit();
1098 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001099 PyErr_Fetch(&exception2, &v2, &tb2);
1100 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001101 /* It should not be possible for exception2 or v2
1102 to be NULL. However PyErr_Display() can't
1103 tolerate NULLs, so just be safe. */
1104 if (exception2 == NULL) {
1105 exception2 = Py_None;
1106 Py_INCREF(exception2);
1107 }
1108 if (v2 == NULL) {
1109 v2 = Py_None;
1110 Py_INCREF(v2);
1111 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001112 if (Py_FlushLine())
1113 PyErr_Clear();
1114 fflush(stdout);
1115 PySys_WriteStderr("Error in sys.excepthook:\n");
1116 PyErr_Display(exception2, v2, tb2);
1117 PySys_WriteStderr("\nOriginal exception was:\n");
1118 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001119 Py_DECREF(exception2);
1120 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001121 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001122 }
1123 Py_XDECREF(result);
1124 Py_XDECREF(args);
1125 } else {
1126 PySys_WriteStderr("sys.excepthook is missing\n");
1127 PyErr_Display(exception, v, tb);
1128 }
1129 Py_XDECREF(exception);
1130 Py_XDECREF(v);
1131 Py_XDECREF(tb);
1132}
1133
Richard Jones7b9558d2006-05-27 12:29:24 +00001134void
1135PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001136{
1137 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001138 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001139 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001140 if (f == NULL)
1141 fprintf(stderr, "lost sys.stderr\n");
1142 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001143 if (Py_FlushLine())
1144 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001145 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001146 if (tb && tb != Py_None)
1147 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001148 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001149 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001150 {
Guido van Rossum82598051997-03-05 00:20:32 +00001151 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001152 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001153 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001154 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001155 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001156 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001157 else {
1158 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001159 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001160 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001161 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001162 else
Guido van Rossum82598051997-03-05 00:20:32 +00001163 PyFile_WriteString(filename, f);
1164 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001165 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001166 PyFile_WriteString(buf, f);
1167 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001168 if (text != NULL)
1169 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001170 Py_DECREF(value);
1171 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001172 /* Can't be bothered to check all those
1173 PyFile_WriteString() calls */
1174 if (PyErr_Occurred())
1175 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001176 }
1177 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001178 if (err) {
1179 /* Don't do anything else */
1180 }
Brett Cannonbf364092006-03-01 04:25:17 +00001181 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001182 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001183 char* className = PyExceptionClass_Name(exception);
1184 if (className != NULL) {
1185 char *dot = strrchr(className, '.');
1186 if (dot != NULL)
1187 className = dot+1;
1188 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001189
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001190 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001191 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001192 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001193 else {
1194 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001195 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001196 {
1197 err = PyFile_WriteString(modstr, f);
1198 err += PyFile_WriteString(".", f);
1199 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001200 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001201 }
1202 if (err == 0) {
1203 if (className == NULL)
1204 err = PyFile_WriteString("<unknown>", f);
1205 else
Brett Cannonbf364092006-03-01 04:25:17 +00001206 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001207 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001208 }
1209 else
1210 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001211 if (err == 0 && (value != Py_None)) {
1212 PyObject *s = PyObject_Str(value);
1213 /* only print colon if the str() of the
1214 object is not the empty string
1215 */
1216 if (s == NULL)
1217 err = -1;
1218 else if (!PyString_Check(s) ||
1219 PyString_GET_SIZE(s) != 0)
1220 err = PyFile_WriteString(": ", f);
1221 if (err == 0)
1222 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1223 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001224 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001225 /* try to write a newline in any case */
1226 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001227 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001228 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001229 /* If an error happened here, don't show it.
1230 XXX This is wrong, but too many callers rely on this behavior. */
1231 if (err != 0)
1232 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001233}
1234
Guido van Rossum82598051997-03-05 00:20:32 +00001235PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001236PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001238{
Neal Norwitze92fba02006-03-04 18:52:26 +00001239 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001240 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001241 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001242 if (arena == NULL)
1243 return NULL;
1244
1245 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001246 if (mod != NULL)
1247 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001248 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001250}
1251
1252PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001253PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001254 PyObject *locals, int closeit, PyCompilerFlags *flags)
1255{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001257 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001258 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001259 if (arena == NULL)
1260 return NULL;
1261
1262 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1263 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001264 if (closeit)
1265 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001267 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001269 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001270 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001271 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001273}
1274
Guido van Rossum82598051997-03-05 00:20:32 +00001275static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001277 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001278{
Guido van Rossum82598051997-03-05 00:20:32 +00001279 PyCodeObject *co;
1280 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001281 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001282 if (co == NULL)
1283 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001284 v = PyEval_EvalCode(co, globals, locals);
1285 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286 return v;
1287}
1288
Guido van Rossum82598051997-03-05 00:20:32 +00001289static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001290run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001292{
Guido van Rossum82598051997-03-05 00:20:32 +00001293 PyCodeObject *co;
1294 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001295 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001296 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001297
Guido van Rossum82598051997-03-05 00:20:32 +00001298 magic = PyMarshal_ReadLongFromFile(fp);
1299 if (magic != PyImport_GetMagicNumber()) {
1300 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001301 "Bad magic number in .pyc file");
1302 return NULL;
1303 }
Guido van Rossum82598051997-03-05 00:20:32 +00001304 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001305 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001306 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001307 if (v == NULL || !PyCode_Check(v)) {
1308 Py_XDECREF(v);
1309 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001310 "Bad code object in .pyc file");
1311 return NULL;
1312 }
Guido van Rossum82598051997-03-05 00:20:32 +00001313 co = (PyCodeObject *)v;
1314 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001315 if (v && flags)
1316 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001317 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001318 return v;
1319}
1320
Guido van Rossum82598051997-03-05 00:20:32 +00001321PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001322Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001323 PyCompilerFlags *flags)
1324{
Guido van Rossum82598051997-03-05 00:20:32 +00001325 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001326 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001327 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001328 if (arena == NULL)
1329 return NULL;
1330
1331 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001333 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001334 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001335 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001336 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001337 PyObject *result = PyAST_mod2obj(mod);
1338 PyArena_Free(arena);
1339 return result;
1340 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001341 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001342 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001343 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001344}
1345
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001346struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001347Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001348{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001349 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001350 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001351 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001352 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001353 if (arena == NULL)
1354 return NULL;
1355
Christian Heimes7f23d862008-03-26 22:51:58 +00001356 flags.cf_flags = 0;
1357
Christian Heimes3c608332008-03-26 22:01:37 +00001358 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001359 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001360 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001361 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001362 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001364 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001365 return st;
1366}
1367
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368/* Preferred access to parser is through AST. */
1369mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001370PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 mod_ty mod;
1374 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001375 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001376
1377 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001378 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001379 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001381 if (flags) {
1382 flags->cf_flags |= iflags & PyCF_MASK;
1383 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 PyNode_Free(n);
1386 return mod;
1387 }
1388 else {
1389 err_input(&err);
1390 return NULL;
1391 }
1392}
1393
1394mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001395PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001396 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001397 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 mod_ty mod;
1400 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001401 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001402
Christian Heimes3c608332008-03-26 22:01:37 +00001403 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1404 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001406 if (flags) {
1407 flags->cf_flags |= iflags & PyCF_MASK;
1408 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001409 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 PyNode_Free(n);
1411 return mod;
1412 }
1413 else {
1414 err_input(&err);
1415 if (errcode)
1416 *errcode = err.error;
1417 return NULL;
1418 }
1419}
1420
Guido van Rossuma110aa61994-08-29 12:50:44 +00001421/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001422
Guido van Rossuma110aa61994-08-29 12:50:44 +00001423node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001424PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001425{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001426 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001427 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1428 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001429 if (n == NULL)
1430 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001431
Guido van Rossuma110aa61994-08-29 12:50:44 +00001432 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001433}
1434
Guido van Rossuma110aa61994-08-29 12:50:44 +00001435/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001436
Guido van Rossuma110aa61994-08-29 12:50:44 +00001437node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001438PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001439{
Tim Petersfe2127d2001-07-16 05:37:24 +00001440 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001441 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1442 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001443 if (n == NULL)
1444 err_input(&err);
1445 return n;
1446}
1447
1448node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001449PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001450 int start, int flags)
1451{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001452 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001453 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1454 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001455 if (n == NULL)
1456 err_input(&err);
1457 return n;
1458}
1459
1460node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001461PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001462{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001463 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001464}
1465
Guido van Rossum66ebd912003-04-17 16:02:26 +00001466/* May want to move a more generalized form of this to parsetok.c or
1467 even parser modules. */
1468
1469void
1470PyParser_SetError(perrdetail *err)
1471{
1472 err_input(err);
1473}
1474
Guido van Rossuma110aa61994-08-29 12:50:44 +00001475/* Set the error appropriate to the given input error code (see errcode.h) */
1476
1477static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001478err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001479{
Fred Drake85f36392000-07-11 17:53:00 +00001480 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001481 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001482 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001483 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001484 switch (err->error) {
1485 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001486 errtype = PyExc_IndentationError;
1487 if (err->expected == INDENT)
1488 msg = "expected an indented block";
1489 else if (err->token == INDENT)
1490 msg = "unexpected indent";
1491 else if (err->token == DEDENT)
1492 msg = "unexpected unindent";
1493 else {
1494 errtype = PyExc_SyntaxError;
1495 msg = "invalid syntax";
1496 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001497 break;
1498 case E_TOKEN:
1499 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001500 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001501 case E_EOFS:
1502 msg = "EOF while scanning triple-quoted string";
1503 break;
1504 case E_EOLS:
1505 msg = "EOL while scanning single-quoted string";
1506 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001507 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001508 if (!PyErr_Occurred())
1509 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001510 return;
1511 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001512 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001513 return;
1514 case E_EOF:
1515 msg = "unexpected EOF while parsing";
1516 break;
Fred Drake85f36392000-07-11 17:53:00 +00001517 case E_TABSPACE:
1518 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001519 msg = "inconsistent use of tabs and spaces in indentation";
1520 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001521 case E_OVERFLOW:
1522 msg = "expression too long";
1523 break;
Fred Drake85f36392000-07-11 17:53:00 +00001524 case E_DEDENT:
1525 errtype = PyExc_IndentationError;
1526 msg = "unindent does not match any outer indentation level";
1527 break;
1528 case E_TOODEEP:
1529 errtype = PyExc_IndentationError;
1530 msg = "too many levels of indentation";
1531 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001532 case E_DECODE: {
1533 PyObject *type, *value, *tb;
1534 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001535 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001536 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001537 if (u != NULL) {
1538 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001539 }
1540 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001541 if (msg == NULL)
1542 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001543 Py_XDECREF(type);
1544 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001545 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001546 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001547 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001548 case E_LINECONT:
1549 msg = "unexpected character after line continuation character";
1550 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001551 default:
1552 fprintf(stderr, "error=%d\n", err->error);
1553 msg = "unknown parsing error";
1554 break;
1555 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 v = Py_BuildValue("(ziiz)", err->filename,
1557 err->lineno, err->offset, err->text);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001558 if (err->text != NULL) {
Tim Petersc9d78aa2006-03-26 23:27:58 +00001559 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 err->text = NULL;
1561 }
1562 w = NULL;
1563 if (v != NULL)
1564 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001565 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001566 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001567 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001568 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001569}
1570
1571/* Print fatal error message and abort */
1572
1573void
Tim Peters7c321a82002-07-09 02:57:01 +00001574Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001575{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001576 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001577#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001578 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001579 OutputDebugString(msg);
1580 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001581#ifdef _DEBUG
1582 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001583#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001584#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001585 abort();
1586}
1587
1588/* Clean up and exit */
1589
Guido van Rossuma110aa61994-08-29 12:50:44 +00001590#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001591#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001592#endif
1593
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001594#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001595static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001596static int nexitfuncs = 0;
1597
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001598int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001599{
1600 if (nexitfuncs >= NEXITFUNCS)
1601 return -1;
1602 exitfuncs[nexitfuncs++] = func;
1603 return 0;
1604}
1605
Guido van Rossumcc283f51997-08-05 02:22:03 +00001606static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001607call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001608{
Guido van Rossum82598051997-03-05 00:20:32 +00001609 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001610
1611 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001612 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001613 Py_INCREF(exitfunc);
1614 PySys_SetObject("exitfunc", (PyObject *)NULL);
1615 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001616 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001617 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1618 PySys_WriteStderr("Error in sys.exitfunc:\n");
1619 }
Guido van Rossum82598051997-03-05 00:20:32 +00001620 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001621 }
Guido van Rossum82598051997-03-05 00:20:32 +00001622 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001623 }
1624
Guido van Rossum0829c751998-02-28 04:31:39 +00001625 if (Py_FlushLine())
1626 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001627}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001628
Guido van Rossumcc283f51997-08-05 02:22:03 +00001629static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001630call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001631{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001632 while (nexitfuncs > 0)
1633 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001634
1635 fflush(stdout);
1636 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001637}
1638
1639void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001640Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001641{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001642 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001643
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001644 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001645}
1646
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001647static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001648initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001649{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001650#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001651 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001652#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001653#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001654 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001655#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001656#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001657 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001658#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001659 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001660}
1661
Guido van Rossum7433b121997-02-14 19:45:36 +00001662
1663/*
1664 * The file descriptor fd is considered ``interactive'' if either
1665 * a) isatty(fd) is TRUE, or
1666 * b) the -i flag was given, and the filename associated with
1667 * the descriptor is NULL or "<stdin>" or "???".
1668 */
1669int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001670Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001671{
1672 if (isatty((int)fileno(fp)))
1673 return 1;
1674 if (!Py_InteractiveFlag)
1675 return 0;
1676 return (filename == NULL) ||
1677 (strcmp(filename, "<stdin>") == 0) ||
1678 (strcmp(filename, "???") == 0);
1679}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001680
1681
Tim Petersd08e3822003-04-17 15:24:21 +00001682#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001683#if defined(WIN32) && defined(_MSC_VER)
1684
1685/* Stack checking for Microsoft C */
1686
1687#include <malloc.h>
1688#include <excpt.h>
1689
Fred Drakee8de31c2000-08-31 05:38:39 +00001690/*
1691 * Return non-zero when we run out of memory on the stack; zero otherwise.
1692 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001693int
Fred Drake399739f2000-08-31 05:52:44 +00001694PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001695{
1696 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001697 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001698 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001699 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001700 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001701 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1702 EXCEPTION_EXECUTE_HANDLER :
1703 EXCEPTION_CONTINUE_SEARCH) {
1704 int errcode = _resetstkoflw();
1705 if (errcode)
1706 {
1707 Py_FatalError("Could not reset the stack!");
1708 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001709 }
1710 return 1;
1711}
1712
1713#endif /* WIN32 && _MSC_VER */
1714
1715/* Alternate implementations can be added here... */
1716
1717#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001718
1719
1720/* Wrappers around sigaction() or signal(). */
1721
1722PyOS_sighandler_t
1723PyOS_getsig(int sig)
1724{
1725#ifdef HAVE_SIGACTION
1726 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001727 if (sigaction(sig, NULL, &context) == -1)
1728 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001729 return context.sa_handler;
1730#else
1731 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001732/* Special signal handling for the secure CRT in Visual Studio 2005 */
1733#if defined(_MSC_VER) && _MSC_VER >= 1400
1734 switch (sig) {
1735 /* Only these signals are valid */
1736 case SIGINT:
1737 case SIGILL:
1738 case SIGFPE:
1739 case SIGSEGV:
1740 case SIGTERM:
1741 case SIGBREAK:
1742 case SIGABRT:
1743 break;
1744 /* Don't call signal() with other values or it will assert */
1745 default:
1746 return SIG_ERR;
1747 }
1748#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001749 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001750 if (handler != SIG_ERR)
1751 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001752 return handler;
1753#endif
1754}
1755
1756PyOS_sighandler_t
1757PyOS_setsig(int sig, PyOS_sighandler_t handler)
1758{
1759#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001760 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001761 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001762 sigemptyset(&context.sa_mask);
1763 context.sa_flags = 0;
1764 if (sigaction(sig, &context, &ocontext) == -1)
1765 return SIG_ERR;
1766 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001767#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001768 PyOS_sighandler_t oldhandler;
1769 oldhandler = signal(sig, handler);
1770#ifdef HAVE_SIGINTERRUPT
1771 siginterrupt(sig, 1);
1772#endif
1773 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001774#endif
1775}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776
1777/* Deprecated C API functions still provided for binary compatiblity */
1778
1779#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001780PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1782{
1783 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1784}
1785
Thomas Heller1b046642006-04-18 18:51:06 +00001786#undef PyParser_SimpleParseString
1787PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788PyParser_SimpleParseString(const char *str, int start)
1789{
1790 return PyParser_SimpleParseStringFlags(str, start, 0);
1791}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001792
Thomas Heller1b046642006-04-18 18:51:06 +00001793#undef PyRun_AnyFile
1794PyAPI_FUNC(int)
1795PyRun_AnyFile(FILE *fp, const char *name)
1796{
1797 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1798}
1799
1800#undef PyRun_AnyFileEx
1801PyAPI_FUNC(int)
1802PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1803{
1804 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1805}
1806
1807#undef PyRun_AnyFileFlags
1808PyAPI_FUNC(int)
1809PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1810{
1811 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1812}
1813
1814#undef PyRun_File
1815PyAPI_FUNC(PyObject *)
1816PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1817{
1818 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1819}
1820
1821#undef PyRun_FileEx
1822PyAPI_FUNC(PyObject *)
1823PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1824{
1825 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1826}
1827
1828#undef PyRun_FileFlags
1829PyAPI_FUNC(PyObject *)
1830PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1831 PyCompilerFlags *flags)
1832{
1833 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1834}
1835
1836#undef PyRun_SimpleFile
1837PyAPI_FUNC(int)
1838PyRun_SimpleFile(FILE *f, const char *p)
1839{
1840 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1841}
1842
1843#undef PyRun_SimpleFileEx
1844PyAPI_FUNC(int)
1845PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1846{
1847 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1848}
1849
1850
1851#undef PyRun_String
1852PyAPI_FUNC(PyObject *)
1853PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1854{
1855 return PyRun_StringFlags(str, s, g, l, NULL);
1856}
1857
1858#undef PyRun_SimpleString
1859PyAPI_FUNC(int)
1860PyRun_SimpleString(const char *s)
1861{
1862 return PyRun_SimpleStringFlags(s, NULL);
1863}
1864
1865#undef Py_CompileString
1866PyAPI_FUNC(PyObject *)
1867Py_CompileString(const char *str, const char *p, int s)
1868{
1869 return Py_CompileStringFlags(str, p, s, NULL);
1870}
1871
1872#undef PyRun_InteractiveOne
1873PyAPI_FUNC(int)
1874PyRun_InteractiveOne(FILE *f, const char *p)
1875{
1876 return PyRun_InteractiveOneFlags(f, p, NULL);
1877}
1878
1879#undef PyRun_InteractiveLoop
1880PyAPI_FUNC(int)
1881PyRun_InteractiveLoop(FILE *f, const char *p)
1882{
1883 return PyRun_InteractiveLoopFlags(f, p, NULL);
1884}
1885
Anthony Baxterac6bd462006-04-13 02:06:09 +00001886#ifdef __cplusplus
1887}
1888#endif
1889