blob: faca12f9ad200063fdef6ddf43610f74d0a0321f [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;
Christian Heimesaf748c32008-05-06 22:41:46 +000085int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000086
Brett Cannone9746892008-04-12 23:44:07 +000087/* PyModule_GetWarningsModule is no longer necessary as of 2.6
88since _warnings is builtin. This API should not be used. */
89PyObject *
90PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000091{
Brett Cannone9746892008-04-12 23:44:07 +000092 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000093}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000094
Guido van Rossum25ce5661997-08-02 03:10:38 +000095static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096
Thomas Wouters7e474022000-07-16 12:04:32 +000097/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000098
99int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000101{
102 return initialized;
103}
104
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105/* Global initializations. Can be undone by Py_Finalize(). Don't
106 call this twice without an intervening Py_Finalize() call. When
107 initializations fail, a fatal error is issued and the function does
108 not return. On return, the first thread and interpreter state have
109 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 Locking: you must hold the interpreter lock while calling this.
112 (If the lock has not yet been initialized, that's equivalent to
113 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000117static int
118add_flag(int flag, const char *envs)
119{
120 int env = atoi(envs);
121 if (flag < env)
122 flag = env;
123 if (flag < 1)
124 flag = 1;
125 return flag;
126}
127
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000129Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 PyThreadState *tstate;
133 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000134 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000135#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
136 char *codeset;
137 char *saved_locale;
138 PyObject *sys_stream, *sys_isatty;
139#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000140 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000141
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000142 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000143 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000144 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000145
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000146 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000147 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000148 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000149 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000150 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000151 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000152 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
153 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000154
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155 interp = PyInterpreterState_New();
156 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000158
Guido van Rossuma027efa1997-05-05 20:56:21 +0000159 tstate = PyThreadState_New(interp);
160 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000161 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000162 (void) PyThreadState_Swap(tstate);
163
Guido van Rossum70d893a2001-08-16 08:21:42 +0000164 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000165
Neal Norwitzb2501f42002-12-31 03:42:13 +0000166 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000167 Py_FatalError("Py_Initialize: can't init frames");
168
Neal Norwitzb2501f42002-12-31 03:42:13 +0000169 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000170 Py_FatalError("Py_Initialize: can't init ints");
171
Christian Heimes1a6387e2008-03-26 12:49:49 +0000172 if (!PyBytes_Init())
173 Py_FatalError("Py_Initialize: can't init bytearray");
174
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000175 _PyFloat_Init();
176
Guido van Rossum25ce5661997-08-02 03:10:38 +0000177 interp->modules = PyDict_New();
178 if (interp->modules == NULL)
179 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000180 interp->modules_reloading = PyDict_New();
181 if (interp->modules_reloading == NULL)
182 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000183
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000184#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000185 /* Init Unicode implementation; relies on the codec registry */
186 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000187#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000188
Barry Warsawf242aa02000-05-25 23:09:49 +0000189 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190 if (bimod == NULL)
191 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000192 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000193 if (interp->builtins == NULL)
194 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000195 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000196
197 sysmod = _PySys_Init();
198 if (sysmod == NULL)
199 Py_FatalError("Py_Initialize: can't initialize sys");
200 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000201 if (interp->sysdict == NULL)
202 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203 Py_INCREF(interp->sysdict);
204 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206 PyDict_SetItemString(interp->sysdict, "modules",
207 interp->modules);
208
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000209 _PyImport_Init();
210
Barry Warsawf242aa02000-05-25 23:09:49 +0000211 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000212 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000213 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000214
Barry Warsaw035574d1997-08-29 22:07:17 +0000215 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000216 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000217
Just van Rossum52e14d62002-12-30 22:08:05 +0000218 _PyImportHooks_Init();
219
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000220 if (install_sigs)
221 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000222
223 /* Initialize warnings. */
224 _PyWarnings_Init();
225 if (PySys_HasWarnOptions()) {
226 PyObject *warnings_module = PyImport_ImportModule("warnings");
227 if (!warnings_module)
228 PyErr_Clear();
229 Py_XDECREF(warnings_module);
230 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231
232 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000233 if (!Py_NoSiteFlag)
234 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000235
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000236 /* auto-thread-state API, if available */
237#ifdef WITH_THREAD
238 _PyGILState_Init(interp, tstate);
239#endif /* WITH_THREAD */
240
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000241#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
242 /* On Unix, set the file system encoding according to the
243 user's preference, if the CODESET names a well-known
244 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000245 initialized by other means. Also set the encoding of
246 stdin and stdout if these are terminals. */
247
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000248 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000249 setlocale(LC_CTYPE, "");
250 codeset = nl_langinfo(CODESET);
251 if (codeset && *codeset) {
252 PyObject *enc = PyCodec_Encoder(codeset);
253 if (enc) {
254 codeset = strdup(codeset);
255 Py_DECREF(enc);
256 } else {
257 codeset = NULL;
258 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000259 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000260 } else
261 codeset = NULL;
262 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000263 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000264
265 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000266 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000267 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
268 if (!sys_isatty)
269 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000270 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
271 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000272 if (!PyFile_SetEncoding(sys_stream, codeset))
273 Py_FatalError("Cannot set codeset of stdin");
274 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000275 Py_XDECREF(sys_isatty);
276
277 sys_stream = PySys_GetObject("stdout");
278 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
279 if (!sys_isatty)
280 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000281 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
282 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000283 if (!PyFile_SetEncoding(sys_stream, codeset))
284 Py_FatalError("Cannot set codeset of stdout");
285 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000286 Py_XDECREF(sys_isatty);
287
Martin v. Löwisea62d252006-04-03 10:56:49 +0000288 sys_stream = PySys_GetObject("stderr");
289 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
290 if (!sys_isatty)
291 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000292 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
293 PyFile_Check(sys_stream)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +0000294 if (!PyFile_SetEncoding(sys_stream, codeset))
295 Py_FatalError("Cannot set codeset of stderr");
296 }
297 Py_XDECREF(sys_isatty);
298
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000299 if (!Py_FileSystemDefaultEncoding)
300 Py_FileSystemDefaultEncoding = codeset;
301 else
302 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000303 }
304#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000305}
306
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000307void
308Py_Initialize(void)
309{
310 Py_InitializeEx(1);
311}
312
313
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000314#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000315extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000316#endif
317
Guido van Rossum25ce5661997-08-02 03:10:38 +0000318/* Undo the effect of Py_Initialize().
319
320 Beware: if multiple interpreter and/or thread states exist, these
321 are not wiped out; only the current thread and interpreter state
322 are deleted. But since everything else is deleted, those other
323 interpreter and thread states should no longer be used.
324
325 (XXX We should do better, e.g. wipe out all interpreters and
326 threads.)
327
328 Locking: as above.
329
330*/
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334{
335 PyInterpreterState *interp;
336 PyThreadState *tstate;
337
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000338 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000339 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000340
Tim Peters384fd102001-01-21 03:40:37 +0000341 /* The interpreter is still entirely intact at this point, and the
342 * exit funcs may be relying on that. In particular, if some thread
343 * or exit func is still waiting to do an import, the import machinery
344 * expects Py_IsInitialized() to return true. So don't say the
345 * interpreter is uninitialized until after the exit funcs have run.
346 * Note that Threading.py uses an exit func to do a join on all the
347 * threads created thru it, so this also protects pending imports in
348 * the threads created via Threading.
349 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000350 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000351 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000352
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000353 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000354 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355 interp = tstate->interp;
356
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000357 /* Disable signal handling */
358 PyOS_FiniInterrupts();
359
Christian Heimes908caac2008-01-27 23:34:59 +0000360 /* Clear type lookup cache */
361 PyType_ClearCache();
362
Guido van Rossume13ddc92003-04-17 17:29:22 +0000363 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000364 * before all modules are destroyed.
365 * XXX If a __del__ or weakref callback is triggered here, and tries to
366 * XXX import a module, bad things can happen, because Python no
367 * XXX longer believes it's initialized.
368 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
369 * XXX is easy to provoke that way. I've also seen, e.g.,
370 * XXX Exception exceptions.ImportError: 'No module named sha'
371 * XXX in <function callback at 0x008F5718> ignored
372 * XXX but I'm unclear on exactly how that one happens. In any case,
373 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000374 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000375 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000376#ifdef COUNT_ALLOCS
377 /* With COUNT_ALLOCS, it helps to run GC multiple times:
378 each collection might release some types from the type
379 list, so they become garbage. */
380 while (PyGC_Collect() > 0)
381 /* nothing */;
382#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000383
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000384 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000386
Guido van Rossume13ddc92003-04-17 17:29:22 +0000387 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000388 * new-style class definitions, for example.
389 * XXX This is disabled because it caused too many problems. If
390 * XXX a __del__ or weakref callback triggers here, Python code has
391 * XXX a hard time running, because even the sys module has been
392 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
393 * XXX One symptom is a sequence of information-free messages
394 * XXX coming from threads (if a __del__ or callback is invoked,
395 * XXX other threads can execute too, and any exception they encounter
396 * XXX triggers a comedy of errors as subsystem after subsystem
397 * XXX fails to find what it *expects* to find in sys to help report
398 * XXX the exception and consequent unexpected failures). I've also
399 * XXX seen segfaults then, after adding print statements to the
400 * XXX Python code getting called.
401 */
402#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000403 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000404#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000405
Guido van Rossum1707aad1997-12-08 23:43:45 +0000406 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
407 _PyImport_Fini();
408
409 /* Debugging stuff */
410#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000411 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000412#endif
413
Tim Peters62e97f02006-03-28 21:44:32 +0000414 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000415
Tim Peters9cf25ce2003-04-17 15:21:01 +0000416#ifdef Py_TRACE_REFS
417 /* Display all objects still alive -- this can invoke arbitrary
418 * __repr__ overrides, so requires a mostly-intact interpreter.
419 * Alas, a lot of stuff may still be alive now that will be cleaned
420 * up later.
421 */
Tim Peters269b2a62003-04-17 19:52:29 +0000422 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000423 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000424#endif /* Py_TRACE_REFS */
425
Guido van Rossumd922fa42003-04-15 14:10:09 +0000426 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000427 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000428
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000429 /* Now we decref the exception classes. After this point nothing
430 can raise an exception. That's okay, because each Fini() method
431 below has been checked to make sure no exceptions are ever
432 raised.
433 */
434
435 _PyExc_Fini();
436
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000437 /* Cleanup auto-thread-state */
438#ifdef WITH_THREAD
439 _PyGILState_Fini();
440#endif /* WITH_THREAD */
441
Guido van Rossumd922fa42003-04-15 14:10:09 +0000442 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000443 PyThreadState_Swap(NULL);
444 PyInterpreterState_Delete(interp);
445
Guido van Rossumd922fa42003-04-15 14:10:09 +0000446 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000447 PyMethod_Fini();
448 PyFrame_Fini();
449 PyCFunction_Fini();
450 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000451 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000452 PySet_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000453 PyString_Fini();
Christian Heimes1a6387e2008-03-26 12:49:49 +0000454 PyBytes_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000455 PyInt_Fini();
456 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000457 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000458
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000459#ifdef Py_USING_UNICODE
460 /* Cleanup Unicode implementation */
461 _PyUnicode_Fini();
462#endif
463
Guido van Rossumcc283f51997-08-05 02:22:03 +0000464 /* XXX Still allocated:
465 - various static ad-hoc pointers to interned strings
466 - int and float free list blocks
467 - whatever various modules and libraries allocate
468 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000469
470 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000471
Tim Peters269b2a62003-04-17 19:52:29 +0000472#ifdef Py_TRACE_REFS
473 /* Display addresses (& refcnts) of all objects still alive.
474 * An address can be used to find the repr of the object, printed
475 * above by _Py_PrintReferences.
476 */
477 if (Py_GETENV("PYTHONDUMPREFS"))
478 _Py_PrintReferenceAddresses(stderr);
479#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000480#ifdef PYMALLOC_DEBUG
481 if (Py_GETENV("PYTHONMALLOCSTATS"))
482 _PyObject_DebugMallocStats();
483#endif
484
Guido van Rossumcc283f51997-08-05 02:22:03 +0000485 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000486}
487
488/* Create and initialize a new interpreter and thread, and return the
489 new thread. This requires that Py_Initialize() has been called
490 first.
491
492 Unsuccessful initialization yields a NULL pointer. Note that *no*
493 exception information is available even in this case -- the
494 exception information is held in the thread, and there is no
495 thread.
496
497 Locking: as above.
498
499*/
500
501PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000502Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000503{
504 PyInterpreterState *interp;
505 PyThreadState *tstate, *save_tstate;
506 PyObject *bimod, *sysmod;
507
508 if (!initialized)
509 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
510
511 interp = PyInterpreterState_New();
512 if (interp == NULL)
513 return NULL;
514
515 tstate = PyThreadState_New(interp);
516 if (tstate == NULL) {
517 PyInterpreterState_Delete(interp);
518 return NULL;
519 }
520
521 save_tstate = PyThreadState_Swap(tstate);
522
523 /* XXX The following is lax in error checking */
524
525 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000526 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000527
528 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
529 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000530 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000531 if (interp->builtins == NULL)
532 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000533 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000534 }
535 sysmod = _PyImport_FindExtension("sys", "sys");
536 if (bimod != NULL && sysmod != NULL) {
537 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000538 if (interp->sysdict == NULL)
539 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540 Py_INCREF(interp->sysdict);
541 PySys_SetPath(Py_GetPath());
542 PyDict_SetItemString(interp->sysdict, "modules",
543 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000544 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000545 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000546 if (!Py_NoSiteFlag)
547 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548 }
549
550 if (!PyErr_Occurred())
551 return tstate;
552
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000553handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000554 /* Oops, it didn't work. Undo it all. */
555
556 PyErr_Print();
557 PyThreadState_Clear(tstate);
558 PyThreadState_Swap(save_tstate);
559 PyThreadState_Delete(tstate);
560 PyInterpreterState_Delete(interp);
561
562 return NULL;
563}
564
565/* Delete an interpreter and its last thread. This requires that the
566 given thread state is current, that the thread has no remaining
567 frames, and that it is its interpreter's only remaining thread.
568 It is a fatal error to violate these constraints.
569
570 (Py_Finalize() doesn't have these constraints -- it zaps
571 everything, regardless.)
572
573 Locking: as above.
574
575*/
576
577void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000578Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000579{
580 PyInterpreterState *interp = tstate->interp;
581
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000582 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583 Py_FatalError("Py_EndInterpreter: thread is not current");
584 if (tstate->frame != NULL)
585 Py_FatalError("Py_EndInterpreter: thread still has a frame");
586 if (tstate != interp->tstate_head || tstate->next != NULL)
587 Py_FatalError("Py_EndInterpreter: not the last thread");
588
589 PyImport_Cleanup();
590 PyInterpreterState_Clear(interp);
591 PyThreadState_Swap(NULL);
592 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000593}
594
595static char *progname = "python";
596
597void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000598Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000599{
600 if (pn && *pn)
601 progname = pn;
602}
603
604char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000605Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000606{
607 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000608}
609
Guido van Rossuma61691e1998-02-06 22:27:24 +0000610static char *default_home = NULL;
611
612void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000613Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000614{
615 default_home = home;
616}
617
618char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000619Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000620{
621 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000622 if (home == NULL && !Py_IgnoreEnvironmentFlag)
623 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000624 return home;
625}
626
Guido van Rossum6135a871995-01-09 17:53:26 +0000627/* Create __main__ module */
628
629static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000630initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000631{
Guido van Rossum82598051997-03-05 00:20:32 +0000632 PyObject *m, *d;
633 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000634 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000635 Py_FatalError("can't create __main__ module");
636 d = PyModule_GetDict(m);
637 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000638 PyObject *bimod = PyImport_ImportModule("__builtin__");
639 if (bimod == NULL ||
640 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000641 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000642 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000643 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000644}
645
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000646/* Import the site module (not into __main__ though) */
647
648static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000650{
651 PyObject *m, *f;
652 m = PyImport_ImportModule("site");
653 if (m == NULL) {
654 f = PySys_GetObject("stderr");
655 if (Py_VerboseFlag) {
656 PyFile_WriteString(
657 "'import site' failed; traceback:\n", f);
658 PyErr_Print();
659 }
660 else {
661 PyFile_WriteString(
662 "'import site' failed; use -v for traceback\n", f);
663 PyErr_Clear();
664 }
665 }
666 else {
667 Py_DECREF(m);
668 }
669}
670
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000671/* Parse input from a file and execute it */
672
673int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000674PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000675 PyCompilerFlags *flags)
676{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000677 if (filename == NULL)
678 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000679 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000680 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000681 if (closeit)
682 fclose(fp);
683 return err;
684 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000685 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000686 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000687}
688
689int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000690PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000691{
Guido van Rossum82598051997-03-05 00:20:32 +0000692 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000693 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000694 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000695
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000696 if (flags == NULL) {
697 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000698 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000699 }
Guido van Rossum82598051997-03-05 00:20:32 +0000700 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000701 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000702 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
703 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000704 }
Guido van Rossum82598051997-03-05 00:20:32 +0000705 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000706 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000707 PySys_SetObject("ps2", v = PyString_FromString("... "));
708 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000709 }
710 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000711 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000712 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000713 if (ret == E_EOF)
714 return 0;
715 /*
716 if (ret == E_NOMEM)
717 return -1;
718 */
719 }
720}
721
Eric Smith7c478942008-03-18 23:45:49 +0000722#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000723/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000724#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000725 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000726 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000727#endif
728#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000729/* Keep an example of flags with future keyword support. */
730#define PARSER_FLAGS(flags) \
731 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000732 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000733 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
734 PyPARSE_PRINT_IS_FUNCTION : 0) \
735 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
736 PyPARSE_UNICODE_LITERALS : 0) \
737 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000738#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000739
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000740int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000741PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000742{
Guido van Rossum82598051997-03-05 00:20:32 +0000743 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000745 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000746 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000748
Guido van Rossum82598051997-03-05 00:20:32 +0000749 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000750 if (v != NULL) {
751 v = PyObject_Str(v);
752 if (v == NULL)
753 PyErr_Clear();
754 else if (PyString_Check(v))
755 ps1 = PyString_AsString(v);
756 }
Guido van Rossum82598051997-03-05 00:20:32 +0000757 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000758 if (w != NULL) {
759 w = PyObject_Str(w);
760 if (w == NULL)
761 PyErr_Clear();
762 else if (PyString_Check(w))
763 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000764 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000765 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000766 if (arena == NULL) {
767 Py_XDECREF(v);
768 Py_XDECREF(w);
769 return -1;
770 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000771 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000773 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000774 Py_XDECREF(v);
775 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000777 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778 if (errcode == E_EOF) {
779 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000780 return E_EOF;
781 }
Guido van Rossum82598051997-03-05 00:20:32 +0000782 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000784 }
Guido van Rossum82598051997-03-05 00:20:32 +0000785 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000786 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000787 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000788 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000789 }
Guido van Rossum82598051997-03-05 00:20:32 +0000790 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000791 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000792 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000793 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000794 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000795 return -1;
796 }
Guido van Rossum82598051997-03-05 00:20:32 +0000797 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000798 if (Py_FlushLine())
799 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000800 return 0;
801}
802
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000803/* Check whether a file maybe a pyc file: Look at the extension,
804 the file type, and, if we may close it, at the first few bytes. */
805
806static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000807maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000808{
809 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
810 return 1;
811
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000812 /* Only look into the file if we are allowed to close it, since
813 it then should also be seekable. */
814 if (closeit) {
815 /* Read only two bytes of the magic. If the file was opened in
816 text mode, the bytes 3 and 4 of the magic (\r\n) might not
817 be read as they are on disk. */
818 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
819 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000820 /* Mess: In case of -x, the stream is NOT at its start now,
821 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000822 which makes the current stream position formally undefined,
823 and a x-platform nightmare.
824 Unfortunately, we have no direct way to know whether -x
825 was specified. So we use a terrible hack: if the current
826 stream position is not 0, we assume -x was specified, and
827 give up. Bug 132850 on SourceForge spells out the
828 hopelessness of trying anything else (fseek and ftell
829 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000830 */
Tim Peters3e876562001-02-11 04:35:39 +0000831 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000832 if (ftell(fp) == 0) {
833 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000834 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000835 ispyc = 1;
836 rewind(fp);
837 }
Tim Peters3e876562001-02-11 04:35:39 +0000838 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000839 }
840 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000841}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000842
Guido van Rossum0df002c2000-08-27 19:21:52 +0000843int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000844PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000845 PyCompilerFlags *flags)
846{
Guido van Rossum82598051997-03-05 00:20:32 +0000847 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000848 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000849 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000850
Guido van Rossum82598051997-03-05 00:20:32 +0000851 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000852 if (m == NULL)
853 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000854 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000855 if (PyDict_GetItemString(d, "__file__") == NULL) {
856 PyObject *f = PyString_FromString(filename);
857 if (f == NULL)
858 return -1;
859 if (PyDict_SetItemString(d, "__file__", f) < 0) {
860 Py_DECREF(f);
861 return -1;
862 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000863 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000864 Py_DECREF(f);
865 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000866 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000867 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000868 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000869 if (closeit)
870 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000871 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000872 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000873 ret = -1;
874 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000875 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000876 /* Turn on optimization if a .pyo file is given */
877 if (strcmp(ext, ".pyo") == 0)
878 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000879 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000880 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000881 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000882 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000883 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000884 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000885 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000886 ret = -1;
887 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000888 }
Guido van Rossum82598051997-03-05 00:20:32 +0000889 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000890 if (Py_FlushLine())
891 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000892 ret = 0;
893 done:
894 if (set_file_name && PyDict_DelItemString(d, "__file__"))
895 PyErr_Clear();
896 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000897}
898
899int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000900PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000901{
Guido van Rossum82598051997-03-05 00:20:32 +0000902 PyObject *m, *d, *v;
903 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000904 if (m == NULL)
905 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000906 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000907 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000908 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000909 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000910 return -1;
911 }
Guido van Rossum82598051997-03-05 00:20:32 +0000912 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000913 if (Py_FlushLine())
914 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000915 return 0;
916}
917
Barry Warsaw035574d1997-08-29 22:07:17 +0000918static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000919parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
920 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000921{
922 long hold;
923 PyObject *v;
924
925 /* old style errors */
926 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000927 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000928 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000929
930 /* new style errors. `err' is an instance */
931
932 if (! (v = PyObject_GetAttrString(err, "msg")))
933 goto finally;
934 *message = v;
935
936 if (!(v = PyObject_GetAttrString(err, "filename")))
937 goto finally;
938 if (v == Py_None)
939 *filename = NULL;
940 else if (! (*filename = PyString_AsString(v)))
941 goto finally;
942
943 Py_DECREF(v);
944 if (!(v = PyObject_GetAttrString(err, "lineno")))
945 goto finally;
946 hold = PyInt_AsLong(v);
947 Py_DECREF(v);
948 v = NULL;
949 if (hold < 0 && PyErr_Occurred())
950 goto finally;
951 *lineno = (int)hold;
952
953 if (!(v = PyObject_GetAttrString(err, "offset")))
954 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000955 if (v == Py_None) {
956 *offset = -1;
957 Py_DECREF(v);
958 v = NULL;
959 } else {
960 hold = PyInt_AsLong(v);
961 Py_DECREF(v);
962 v = NULL;
963 if (hold < 0 && PyErr_Occurred())
964 goto finally;
965 *offset = (int)hold;
966 }
Barry Warsaw035574d1997-08-29 22:07:17 +0000967
968 if (!(v = PyObject_GetAttrString(err, "text")))
969 goto finally;
970 if (v == Py_None)
971 *text = NULL;
972 else if (! (*text = PyString_AsString(v)))
973 goto finally;
974 Py_DECREF(v);
975 return 1;
976
977finally:
978 Py_XDECREF(v);
979 return 0;
980}
981
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000982void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000983PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000984{
Guido van Rossuma61691e1998-02-06 22:27:24 +0000985 PyErr_PrintEx(1);
986}
987
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000988static void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000989print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000990{
991 char *nl;
992 if (offset >= 0) {
993 if (offset > 0 && offset == (int)strlen(text))
994 offset--;
995 for (;;) {
996 nl = strchr(text, '\n');
997 if (nl == NULL || nl-text >= offset)
998 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000999 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001000 text = nl+1;
1001 }
1002 while (*text == ' ' || *text == '\t') {
1003 text++;
1004 offset--;
1005 }
1006 }
1007 PyFile_WriteString(" ", f);
1008 PyFile_WriteString(text, f);
1009 if (*text == '\0' || text[strlen(text)-1] != '\n')
1010 PyFile_WriteString("\n", f);
1011 if (offset == -1)
1012 return;
1013 PyFile_WriteString(" ", f);
1014 offset--;
1015 while (offset > 0) {
1016 PyFile_WriteString(" ", f);
1017 offset--;
1018 }
1019 PyFile_WriteString("^\n", f);
1020}
1021
Guido van Rossum66e8e862001-03-23 17:54:43 +00001022static void
1023handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001024{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001025 PyObject *exception, *value, *tb;
1026 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001027
Georg Brandl49aafc92007-03-07 00:34:46 +00001028 if (Py_InspectFlag)
1029 /* Don't exit if -i flag was given. This flag is set to 0
1030 * when entering interactive mode for inspecting. */
1031 return;
1032
Guido van Rossum66e8e862001-03-23 17:54:43 +00001033 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001034 if (Py_FlushLine())
1035 PyErr_Clear();
1036 fflush(stdout);
1037 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001038 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001039 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001040 /* The error code should be in the `code' attribute. */
1041 PyObject *code = PyObject_GetAttrString(value, "code");
1042 if (code) {
1043 Py_DECREF(value);
1044 value = code;
1045 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001046 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001047 }
1048 /* If we failed to dig out the 'code' attribute,
1049 just let the else clause below print the error. */
1050 }
1051 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001052 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001053 else {
1054 PyObject_Print(value, stderr, Py_PRINT_RAW);
1055 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001056 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001057 }
Tim Peterscf615b52003-04-19 18:47:02 +00001058 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001059 /* Restore and clear the exception info, in order to properly decref
1060 * the exception, value, and traceback. If we just exit instead,
1061 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1062 * some finalizers from running.
1063 */
Tim Peterscf615b52003-04-19 18:47:02 +00001064 PyErr_Restore(exception, value, tb);
1065 PyErr_Clear();
1066 Py_Exit(exitcode);
1067 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001068}
1069
1070void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001071PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001072{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001073 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001074
1075 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1076 handle_system_exit();
1077 }
Guido van Rossum82598051997-03-05 00:20:32 +00001078 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 if (exception == NULL)
1080 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001081 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001082 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001083 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001084 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001085 if (set_sys_last_vars) {
1086 PySys_SetObject("last_type", exception);
1087 PySys_SetObject("last_value", v);
1088 PySys_SetObject("last_traceback", tb);
1089 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001090 hook = PySys_GetObject("excepthook");
1091 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001092 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001093 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001094 PyObject *result = PyEval_CallObject(hook, args);
1095 if (result == NULL) {
1096 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001097 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1098 handle_system_exit();
1099 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001100 PyErr_Fetch(&exception2, &v2, &tb2);
1101 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001102 /* It should not be possible for exception2 or v2
1103 to be NULL. However PyErr_Display() can't
1104 tolerate NULLs, so just be safe. */
1105 if (exception2 == NULL) {
1106 exception2 = Py_None;
1107 Py_INCREF(exception2);
1108 }
1109 if (v2 == NULL) {
1110 v2 = Py_None;
1111 Py_INCREF(v2);
1112 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001113 if (Py_FlushLine())
1114 PyErr_Clear();
1115 fflush(stdout);
1116 PySys_WriteStderr("Error in sys.excepthook:\n");
1117 PyErr_Display(exception2, v2, tb2);
1118 PySys_WriteStderr("\nOriginal exception was:\n");
1119 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001120 Py_DECREF(exception2);
1121 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001122 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001123 }
1124 Py_XDECREF(result);
1125 Py_XDECREF(args);
1126 } else {
1127 PySys_WriteStderr("sys.excepthook is missing\n");
1128 PyErr_Display(exception, v, tb);
1129 }
1130 Py_XDECREF(exception);
1131 Py_XDECREF(v);
1132 Py_XDECREF(tb);
1133}
1134
Richard Jones7b9558d2006-05-27 12:29:24 +00001135void
1136PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001137{
1138 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001139 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001140 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001141 if (f == NULL)
1142 fprintf(stderr, "lost sys.stderr\n");
1143 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001144 if (Py_FlushLine())
1145 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001146 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001147 if (tb && tb != Py_None)
1148 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001149 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001150 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001151 {
Guido van Rossum82598051997-03-05 00:20:32 +00001152 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001153 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001154 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001155 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001156 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001157 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001158 else {
1159 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001160 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001161 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001162 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001163 else
Guido van Rossum82598051997-03-05 00:20:32 +00001164 PyFile_WriteString(filename, f);
1165 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001166 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001167 PyFile_WriteString(buf, f);
1168 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001169 if (text != NULL)
1170 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001171 Py_DECREF(value);
1172 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001173 /* Can't be bothered to check all those
1174 PyFile_WriteString() calls */
1175 if (PyErr_Occurred())
1176 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001177 }
1178 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001179 if (err) {
1180 /* Don't do anything else */
1181 }
Brett Cannonbf364092006-03-01 04:25:17 +00001182 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001183 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001184 char* className = PyExceptionClass_Name(exception);
1185 if (className != NULL) {
1186 char *dot = strrchr(className, '.');
1187 if (dot != NULL)
1188 className = dot+1;
1189 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001190
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001191 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001192 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001193 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001194 else {
1195 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001196 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001197 {
1198 err = PyFile_WriteString(modstr, f);
1199 err += PyFile_WriteString(".", f);
1200 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001201 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001202 }
1203 if (err == 0) {
1204 if (className == NULL)
1205 err = PyFile_WriteString("<unknown>", f);
1206 else
Brett Cannonbf364092006-03-01 04:25:17 +00001207 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001208 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001209 }
1210 else
1211 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001212 if (err == 0 && (value != Py_None)) {
1213 PyObject *s = PyObject_Str(value);
1214 /* only print colon if the str() of the
1215 object is not the empty string
1216 */
1217 if (s == NULL)
1218 err = -1;
1219 else if (!PyString_Check(s) ||
1220 PyString_GET_SIZE(s) != 0)
1221 err = PyFile_WriteString(": ", f);
1222 if (err == 0)
1223 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1224 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001225 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001226 /* try to write a newline in any case */
1227 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001228 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001229 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001230 /* If an error happened here, don't show it.
1231 XXX This is wrong, but too many callers rely on this behavior. */
1232 if (err != 0)
1233 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001234}
1235
Guido van Rossum82598051997-03-05 00:20:32 +00001236PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001237PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001239{
Neal Norwitze92fba02006-03-04 18:52:26 +00001240 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001241 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001242 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001243 if (arena == NULL)
1244 return NULL;
1245
1246 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001247 if (mod != NULL)
1248 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001249 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001251}
1252
1253PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001254PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001255 PyObject *locals, int closeit, PyCompilerFlags *flags)
1256{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001258 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001259 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001260 if (arena == NULL)
1261 return NULL;
1262
1263 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1264 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001265 if (closeit)
1266 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001267 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001268 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001270 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001271 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001272 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001274}
1275
Guido van Rossum82598051997-03-05 00:20:32 +00001276static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001279{
Guido van Rossum82598051997-03-05 00:20:32 +00001280 PyCodeObject *co;
1281 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001282 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001283 if (co == NULL)
1284 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001285 v = PyEval_EvalCode(co, globals, locals);
1286 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001287 return v;
1288}
1289
Guido van Rossum82598051997-03-05 00:20:32 +00001290static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001291run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001293{
Guido van Rossum82598051997-03-05 00:20:32 +00001294 PyCodeObject *co;
1295 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001296 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001297 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001298
Guido van Rossum82598051997-03-05 00:20:32 +00001299 magic = PyMarshal_ReadLongFromFile(fp);
1300 if (magic != PyImport_GetMagicNumber()) {
1301 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001302 "Bad magic number in .pyc file");
1303 return NULL;
1304 }
Guido van Rossum82598051997-03-05 00:20:32 +00001305 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001306 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001307 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001308 if (v == NULL || !PyCode_Check(v)) {
1309 Py_XDECREF(v);
1310 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001311 "Bad code object in .pyc file");
1312 return NULL;
1313 }
Guido van Rossum82598051997-03-05 00:20:32 +00001314 co = (PyCodeObject *)v;
1315 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001316 if (v && flags)
1317 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001318 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001319 return v;
1320}
1321
Guido van Rossum82598051997-03-05 00:20:32 +00001322PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001323Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001324 PyCompilerFlags *flags)
1325{
Guido van Rossum82598051997-03-05 00:20:32 +00001326 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001327 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001328 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001329 if (arena == NULL)
1330 return NULL;
1331
1332 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001334 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001335 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001336 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001337 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001338 PyObject *result = PyAST_mod2obj(mod);
1339 PyArena_Free(arena);
1340 return result;
1341 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001343 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001344 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001345}
1346
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001347struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001348Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001349{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001350 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001351 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001352 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001353 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001354 if (arena == NULL)
1355 return NULL;
1356
Christian Heimes7f23d862008-03-26 22:51:58 +00001357 flags.cf_flags = 0;
1358
Christian Heimes3c608332008-03-26 22:01:37 +00001359 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001360 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001361 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001362 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001365 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001366 return st;
1367}
1368
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369/* Preferred access to parser is through AST. */
1370mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001371PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001372 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 mod_ty mod;
1375 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001376 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001377
1378 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001379 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001380 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001382 if (flags) {
1383 flags->cf_flags |= iflags & PyCF_MASK;
1384 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001385 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 PyNode_Free(n);
1387 return mod;
1388 }
1389 else {
1390 err_input(&err);
1391 return NULL;
1392 }
1393}
1394
1395mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001396PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001397 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001398 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 mod_ty mod;
1401 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001402 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001403
Christian Heimes3c608332008-03-26 22:01:37 +00001404 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1405 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001407 if (flags) {
1408 flags->cf_flags |= iflags & PyCF_MASK;
1409 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 PyNode_Free(n);
1412 return mod;
1413 }
1414 else {
1415 err_input(&err);
1416 if (errcode)
1417 *errcode = err.error;
1418 return NULL;
1419 }
1420}
1421
Guido van Rossuma110aa61994-08-29 12:50:44 +00001422/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001423
Guido van Rossuma110aa61994-08-29 12:50:44 +00001424node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001425PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001426{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001427 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001428 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1429 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001430 if (n == NULL)
1431 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001432
Guido van Rossuma110aa61994-08-29 12:50:44 +00001433 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001434}
1435
Guido van Rossuma110aa61994-08-29 12:50:44 +00001436/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001437
Guido van Rossuma110aa61994-08-29 12:50:44 +00001438node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001439PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001440{
Tim Petersfe2127d2001-07-16 05:37:24 +00001441 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1443 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001444 if (n == NULL)
1445 err_input(&err);
1446 return n;
1447}
1448
1449node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001450PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001451 int start, int flags)
1452{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001453 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001454 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1455 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001456 if (n == NULL)
1457 err_input(&err);
1458 return n;
1459}
1460
1461node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001462PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001463{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001464 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001465}
1466
Guido van Rossum66ebd912003-04-17 16:02:26 +00001467/* May want to move a more generalized form of this to parsetok.c or
1468 even parser modules. */
1469
1470void
1471PyParser_SetError(perrdetail *err)
1472{
1473 err_input(err);
1474}
1475
Guido van Rossuma110aa61994-08-29 12:50:44 +00001476/* Set the error appropriate to the given input error code (see errcode.h) */
1477
1478static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001479err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001480{
Fred Drake85f36392000-07-11 17:53:00 +00001481 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001482 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001483 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001484 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001485 switch (err->error) {
1486 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001487 errtype = PyExc_IndentationError;
1488 if (err->expected == INDENT)
1489 msg = "expected an indented block";
1490 else if (err->token == INDENT)
1491 msg = "unexpected indent";
1492 else if (err->token == DEDENT)
1493 msg = "unexpected unindent";
1494 else {
1495 errtype = PyExc_SyntaxError;
1496 msg = "invalid syntax";
1497 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001498 break;
1499 case E_TOKEN:
1500 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001501 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001502 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001503 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001504 break;
1505 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001506 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001507 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001508 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001509 if (!PyErr_Occurred())
1510 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001511 return;
1512 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001513 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001514 return;
1515 case E_EOF:
1516 msg = "unexpected EOF while parsing";
1517 break;
Fred Drake85f36392000-07-11 17:53:00 +00001518 case E_TABSPACE:
1519 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001520 msg = "inconsistent use of tabs and spaces in indentation";
1521 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001522 case E_OVERFLOW:
1523 msg = "expression too long";
1524 break;
Fred Drake85f36392000-07-11 17:53:00 +00001525 case E_DEDENT:
1526 errtype = PyExc_IndentationError;
1527 msg = "unindent does not match any outer indentation level";
1528 break;
1529 case E_TOODEEP:
1530 errtype = PyExc_IndentationError;
1531 msg = "too many levels of indentation";
1532 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001533 case E_DECODE: {
1534 PyObject *type, *value, *tb;
1535 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001536 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001537 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001538 if (u != NULL) {
1539 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001540 }
1541 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001542 if (msg == NULL)
1543 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001544 Py_XDECREF(type);
1545 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001546 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001547 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001548 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001549 case E_LINECONT:
1550 msg = "unexpected character after line continuation character";
1551 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001552 default:
1553 fprintf(stderr, "error=%d\n", err->error);
1554 msg = "unknown parsing error";
1555 break;
1556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 v = Py_BuildValue("(ziiz)", err->filename,
1558 err->lineno, err->offset, err->text);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001559 if (err->text != NULL) {
Tim Petersc9d78aa2006-03-26 23:27:58 +00001560 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561 err->text = NULL;
1562 }
1563 w = NULL;
1564 if (v != NULL)
1565 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001566 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001567 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001568 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001569 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001570}
1571
1572/* Print fatal error message and abort */
1573
1574void
Tim Peters7c321a82002-07-09 02:57:01 +00001575Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001576{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001577 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001578#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001579 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001580 OutputDebugString(msg);
1581 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001582#ifdef _DEBUG
1583 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001584#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001585#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001586 abort();
1587}
1588
1589/* Clean up and exit */
1590
Guido van Rossuma110aa61994-08-29 12:50:44 +00001591#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001592#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001593#endif
1594
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001595#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001596static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001597static int nexitfuncs = 0;
1598
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001599int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001600{
1601 if (nexitfuncs >= NEXITFUNCS)
1602 return -1;
1603 exitfuncs[nexitfuncs++] = func;
1604 return 0;
1605}
1606
Guido van Rossumcc283f51997-08-05 02:22:03 +00001607static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001608call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001609{
Guido van Rossum82598051997-03-05 00:20:32 +00001610 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001611
1612 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001613 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001614 Py_INCREF(exitfunc);
1615 PySys_SetObject("exitfunc", (PyObject *)NULL);
1616 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001617 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001618 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1619 PySys_WriteStderr("Error in sys.exitfunc:\n");
1620 }
Guido van Rossum82598051997-03-05 00:20:32 +00001621 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001622 }
Guido van Rossum82598051997-03-05 00:20:32 +00001623 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001624 }
1625
Guido van Rossum0829c751998-02-28 04:31:39 +00001626 if (Py_FlushLine())
1627 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001628}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001629
Guido van Rossumcc283f51997-08-05 02:22:03 +00001630static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001631call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001632{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001633 while (nexitfuncs > 0)
1634 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001635
1636 fflush(stdout);
1637 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001638}
1639
1640void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001641Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001642{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001643 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001644
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001645 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001646}
1647
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001648static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001649initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001650{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001651#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001652 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001653#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001654#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001655 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001656#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001657#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001658 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001659#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001660 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001661}
1662
Guido van Rossum7433b121997-02-14 19:45:36 +00001663
1664/*
1665 * The file descriptor fd is considered ``interactive'' if either
1666 * a) isatty(fd) is TRUE, or
1667 * b) the -i flag was given, and the filename associated with
1668 * the descriptor is NULL or "<stdin>" or "???".
1669 */
1670int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001671Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001672{
1673 if (isatty((int)fileno(fp)))
1674 return 1;
1675 if (!Py_InteractiveFlag)
1676 return 0;
1677 return (filename == NULL) ||
1678 (strcmp(filename, "<stdin>") == 0) ||
1679 (strcmp(filename, "???") == 0);
1680}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001681
1682
Tim Petersd08e3822003-04-17 15:24:21 +00001683#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001684#if defined(WIN32) && defined(_MSC_VER)
1685
1686/* Stack checking for Microsoft C */
1687
1688#include <malloc.h>
1689#include <excpt.h>
1690
Fred Drakee8de31c2000-08-31 05:38:39 +00001691/*
1692 * Return non-zero when we run out of memory on the stack; zero otherwise.
1693 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001694int
Fred Drake399739f2000-08-31 05:52:44 +00001695PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001696{
1697 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001698 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001699 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001700 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001701 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001702 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1703 EXCEPTION_EXECUTE_HANDLER :
1704 EXCEPTION_CONTINUE_SEARCH) {
1705 int errcode = _resetstkoflw();
1706 if (errcode)
1707 {
1708 Py_FatalError("Could not reset the stack!");
1709 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001710 }
1711 return 1;
1712}
1713
1714#endif /* WIN32 && _MSC_VER */
1715
1716/* Alternate implementations can be added here... */
1717
1718#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001719
1720
1721/* Wrappers around sigaction() or signal(). */
1722
1723PyOS_sighandler_t
1724PyOS_getsig(int sig)
1725{
1726#ifdef HAVE_SIGACTION
1727 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001728 if (sigaction(sig, NULL, &context) == -1)
1729 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001730 return context.sa_handler;
1731#else
1732 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001733/* Special signal handling for the secure CRT in Visual Studio 2005 */
1734#if defined(_MSC_VER) && _MSC_VER >= 1400
1735 switch (sig) {
1736 /* Only these signals are valid */
1737 case SIGINT:
1738 case SIGILL:
1739 case SIGFPE:
1740 case SIGSEGV:
1741 case SIGTERM:
1742 case SIGBREAK:
1743 case SIGABRT:
1744 break;
1745 /* Don't call signal() with other values or it will assert */
1746 default:
1747 return SIG_ERR;
1748 }
1749#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001750 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001751 if (handler != SIG_ERR)
1752 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001753 return handler;
1754#endif
1755}
1756
1757PyOS_sighandler_t
1758PyOS_setsig(int sig, PyOS_sighandler_t handler)
1759{
1760#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001761 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001762 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001763 sigemptyset(&context.sa_mask);
1764 context.sa_flags = 0;
1765 if (sigaction(sig, &context, &ocontext) == -1)
1766 return SIG_ERR;
1767 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001768#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001769 PyOS_sighandler_t oldhandler;
1770 oldhandler = signal(sig, handler);
1771#ifdef HAVE_SIGINTERRUPT
1772 siginterrupt(sig, 1);
1773#endif
1774 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001775#endif
1776}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777
1778/* Deprecated C API functions still provided for binary compatiblity */
1779
1780#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001781PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1783{
1784 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1785}
1786
Thomas Heller1b046642006-04-18 18:51:06 +00001787#undef PyParser_SimpleParseString
1788PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789PyParser_SimpleParseString(const char *str, int start)
1790{
1791 return PyParser_SimpleParseStringFlags(str, start, 0);
1792}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001793
Thomas Heller1b046642006-04-18 18:51:06 +00001794#undef PyRun_AnyFile
1795PyAPI_FUNC(int)
1796PyRun_AnyFile(FILE *fp, const char *name)
1797{
1798 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1799}
1800
1801#undef PyRun_AnyFileEx
1802PyAPI_FUNC(int)
1803PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1804{
1805 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1806}
1807
1808#undef PyRun_AnyFileFlags
1809PyAPI_FUNC(int)
1810PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1811{
1812 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1813}
1814
1815#undef PyRun_File
1816PyAPI_FUNC(PyObject *)
1817PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1818{
1819 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1820}
1821
1822#undef PyRun_FileEx
1823PyAPI_FUNC(PyObject *)
1824PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1825{
1826 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1827}
1828
1829#undef PyRun_FileFlags
1830PyAPI_FUNC(PyObject *)
1831PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1832 PyCompilerFlags *flags)
1833{
1834 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1835}
1836
1837#undef PyRun_SimpleFile
1838PyAPI_FUNC(int)
1839PyRun_SimpleFile(FILE *f, const char *p)
1840{
1841 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1842}
1843
1844#undef PyRun_SimpleFileEx
1845PyAPI_FUNC(int)
1846PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1847{
1848 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1849}
1850
1851
1852#undef PyRun_String
1853PyAPI_FUNC(PyObject *)
1854PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1855{
1856 return PyRun_StringFlags(str, s, g, l, NULL);
1857}
1858
1859#undef PyRun_SimpleString
1860PyAPI_FUNC(int)
1861PyRun_SimpleString(const char *s)
1862{
1863 return PyRun_SimpleStringFlags(s, NULL);
1864}
1865
1866#undef Py_CompileString
1867PyAPI_FUNC(PyObject *)
1868Py_CompileString(const char *str, const char *p, int s)
1869{
1870 return Py_CompileStringFlags(str, p, s, NULL);
1871}
1872
1873#undef PyRun_InteractiveOne
1874PyAPI_FUNC(int)
1875PyRun_InteractiveOne(FILE *f, const char *p)
1876{
1877 return PyRun_InteractiveOneFlags(f, p, NULL);
1878}
1879
1880#undef PyRun_InteractiveLoop
1881PyAPI_FUNC(int)
1882PyRun_InteractiveLoop(FILE *f, const char *p)
1883{
1884 return PyRun_InteractiveLoopFlags(f, p, NULL);
1885}
1886
Anthony Baxterac6bd462006-04-13 02:06:09 +00001887#ifdef __cplusplus
1888}
1889#endif
1890