blob: 39d7327dd86e13b9287f46d55abf36bf95f8b5e0 [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
Mark Hammonda43fd0c2003-02-19 00:33:33 +000086/* Reference to 'warnings' module, to avoid importing it
Mark Hammondedd07732003-07-15 23:03:55 +000087 on the fly when the import lock may be held. See 683658/771097
Mark Hammonda43fd0c2003-02-19 00:33:33 +000088*/
Mark Hammondedd07732003-07-15 23:03:55 +000089static PyObject *warnings_module = NULL;
90
91/* Returns a borrowed reference to the 'warnings' module, or NULL.
92 If the module is returned, it is guaranteed to have been obtained
93 without acquiring the import lock
94*/
Martin v. Löwisa2c17c52003-08-09 09:47:11 +000095PyObject *PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000096{
97 PyObject *typ, *val, *tb;
98 PyObject *all_modules;
99 /* If we managed to get the module at init time, just use it */
100 if (warnings_module)
101 return warnings_module;
102 /* If it wasn't available at init time, it may be available
103 now in sys.modules (common scenario is frozen apps: import
104 at init time fails, but the frozen init code sets up sys.path
105 correctly, then does an implicit import of warnings for us
106 */
107 /* Save and restore any exceptions */
108 PyErr_Fetch(&typ, &val, &tb);
109
Mark Hammond5f4e8ca2003-07-16 01:54:38 +0000110 all_modules = PySys_GetObject("modules");
Mark Hammondedd07732003-07-15 23:03:55 +0000111 if (all_modules) {
112 warnings_module = PyDict_GetItemString(all_modules, "warnings");
113 /* We keep a ref in the global */
114 Py_XINCREF(warnings_module);
115 }
116 PyErr_Restore(typ, val, tb);
117 return warnings_module;
118}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000119
Guido van Rossum25ce5661997-08-02 03:10:38 +0000120static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000121
Thomas Wouters7e474022000-07-16 12:04:32 +0000122/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000123
124int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000125Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000126{
127 return initialized;
128}
129
Guido van Rossum25ce5661997-08-02 03:10:38 +0000130/* Global initializations. Can be undone by Py_Finalize(). Don't
131 call this twice without an intervening Py_Finalize() call. When
132 initializations fail, a fatal error is issued and the function does
133 not return. On return, the first thread and interpreter state have
134 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000135
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136 Locking: you must hold the interpreter lock while calling this.
137 (If the lock has not yet been initialized, that's equivalent to
138 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000139
Guido van Rossum25ce5661997-08-02 03:10:38 +0000140*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000141
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000142static int
143add_flag(int flag, const char *envs)
144{
145 int env = atoi(envs);
146 if (flag < env)
147 flag = env;
148 if (flag < 1)
149 flag = 1;
150 return flag;
151}
152
Guido van Rossuma027efa1997-05-05 20:56:21 +0000153void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000154Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000155{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000156 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000157 PyThreadState *tstate;
158 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000159 char *p;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000160#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
161 char *codeset;
162 char *saved_locale;
163 PyObject *sys_stream, *sys_isatty;
164#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000165 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000166
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000167 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000168 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000169 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000170
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000171 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000172 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000173 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000174 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000175 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000176 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000177 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
178 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000179
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180 interp = PyInterpreterState_New();
181 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000183
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184 tstate = PyThreadState_New(interp);
185 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000187 (void) PyThreadState_Swap(tstate);
188
Guido van Rossum70d893a2001-08-16 08:21:42 +0000189 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000190
Neal Norwitzb2501f42002-12-31 03:42:13 +0000191 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000192 Py_FatalError("Py_Initialize: can't init frames");
193
Neal Norwitzb2501f42002-12-31 03:42:13 +0000194 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000195 Py_FatalError("Py_Initialize: can't init ints");
196
Christian Heimes1a6387e2008-03-26 12:49:49 +0000197 if (!PyBytes_Init())
198 Py_FatalError("Py_Initialize: can't init bytearray");
199
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000200 _PyFloat_Init();
201
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 interp->modules = PyDict_New();
203 if (interp->modules == NULL)
204 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000205 interp->modules_reloading = PyDict_New();
206 if (interp->modules_reloading == NULL)
207 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000209#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000210 /* Init Unicode implementation; relies on the codec registry */
211 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000212#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000213
Barry Warsawf242aa02000-05-25 23:09:49 +0000214 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000215 if (bimod == NULL)
216 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000217 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000218 if (interp->builtins == NULL)
219 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000220 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000221
222 sysmod = _PySys_Init();
223 if (sysmod == NULL)
224 Py_FatalError("Py_Initialize: can't initialize sys");
225 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000226 if (interp->sysdict == NULL)
227 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000228 Py_INCREF(interp->sysdict);
229 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000230 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000231 PyDict_SetItemString(interp->sysdict, "modules",
232 interp->modules);
233
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000234 _PyImport_Init();
235
Barry Warsawf242aa02000-05-25 23:09:49 +0000236 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000237 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000238 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000239
Barry Warsaw035574d1997-08-29 22:07:17 +0000240 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000241 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000242
Just van Rossum52e14d62002-12-30 22:08:05 +0000243 _PyImportHooks_Init();
244
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000245 if (install_sigs)
246 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247
248 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000249 if (!Py_NoSiteFlag)
250 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000251
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000252 /* auto-thread-state API, if available */
253#ifdef WITH_THREAD
254 _PyGILState_Init(interp, tstate);
255#endif /* WITH_THREAD */
256
Mark Hammondedd07732003-07-15 23:03:55 +0000257 warnings_module = PyImport_ImportModule("warnings");
Christian Heimes1a6387e2008-03-26 12:49:49 +0000258 if (!warnings_module) {
Mark Hammondedd07732003-07-15 23:03:55 +0000259 PyErr_Clear();
Christian Heimes1a6387e2008-03-26 12:49:49 +0000260 }
261 else {
262 PyObject *o;
263 char *action[8];
264
265 if (Py_BytesWarningFlag > 1)
266 *action = "error";
267 else if (Py_BytesWarningFlag)
268 *action = "default";
269 else
270 *action = "ignore";
271
272 o = PyObject_CallMethod(warnings_module,
273 "simplefilter", "sO",
274 *action, PyExc_BytesWarning);
275 if (o == NULL)
276 Py_FatalError("Py_Initialize: can't initialize"
277 "warning filter for BytesWarning.");
278 Py_DECREF(o);
279 }
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000280
281#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
282 /* On Unix, set the file system encoding according to the
283 user's preference, if the CODESET names a well-known
284 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000285 initialized by other means. Also set the encoding of
286 stdin and stdout if these are terminals. */
287
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000288 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000289 setlocale(LC_CTYPE, "");
290 codeset = nl_langinfo(CODESET);
291 if (codeset && *codeset) {
292 PyObject *enc = PyCodec_Encoder(codeset);
293 if (enc) {
294 codeset = strdup(codeset);
295 Py_DECREF(enc);
296 } else {
297 codeset = NULL;
298 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000299 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000300 } else
301 codeset = NULL;
302 setlocale(LC_CTYPE, saved_locale);
Martin v. Löwisf56d0152003-11-13 07:43:21 +0000303 free(saved_locale);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000304
305 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000306 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000307 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
308 if (!sys_isatty)
309 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000310 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
311 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000312 if (!PyFile_SetEncoding(sys_stream, codeset))
313 Py_FatalError("Cannot set codeset of stdin");
314 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000315 Py_XDECREF(sys_isatty);
316
317 sys_stream = PySys_GetObject("stdout");
318 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
319 if (!sys_isatty)
320 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000321 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
322 PyFile_Check(sys_stream)) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000323 if (!PyFile_SetEncoding(sys_stream, codeset))
324 Py_FatalError("Cannot set codeset of stdout");
325 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000326 Py_XDECREF(sys_isatty);
327
Martin v. Löwisea62d252006-04-03 10:56:49 +0000328 sys_stream = PySys_GetObject("stderr");
329 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
330 if (!sys_isatty)
331 PyErr_Clear();
Thomas Woutersafea5292007-01-23 13:42:00 +0000332 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
333 PyFile_Check(sys_stream)) {
Martin v. Löwisea62d252006-04-03 10:56:49 +0000334 if (!PyFile_SetEncoding(sys_stream, codeset))
335 Py_FatalError("Cannot set codeset of stderr");
336 }
337 Py_XDECREF(sys_isatty);
338
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000339 if (!Py_FileSystemDefaultEncoding)
340 Py_FileSystemDefaultEncoding = codeset;
341 else
342 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000343 }
344#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000345}
346
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000347void
348Py_Initialize(void)
349{
350 Py_InitializeEx(1);
351}
352
353
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000354#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000355extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000356#endif
357
Guido van Rossum25ce5661997-08-02 03:10:38 +0000358/* Undo the effect of Py_Initialize().
359
360 Beware: if multiple interpreter and/or thread states exist, these
361 are not wiped out; only the current thread and interpreter state
362 are deleted. But since everything else is deleted, those other
363 interpreter and thread states should no longer be used.
364
365 (XXX We should do better, e.g. wipe out all interpreters and
366 threads.)
367
368 Locking: as above.
369
370*/
371
372void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000373Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000374{
375 PyInterpreterState *interp;
376 PyThreadState *tstate;
377
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000378 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000379 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000380
Tim Peters384fd102001-01-21 03:40:37 +0000381 /* The interpreter is still entirely intact at this point, and the
382 * exit funcs may be relying on that. In particular, if some thread
383 * or exit func is still waiting to do an import, the import machinery
384 * expects Py_IsInitialized() to return true. So don't say the
385 * interpreter is uninitialized until after the exit funcs have run.
386 * Note that Threading.py uses an exit func to do a join on all the
387 * threads created thru it, so this also protects pending imports in
388 * the threads created via Threading.
389 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000390 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000391 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000392
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000393 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000394 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000395 interp = tstate->interp;
396
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000397 /* Disable signal handling */
398 PyOS_FiniInterrupts();
399
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000400 /* drop module references we saved */
Mark Hammondedd07732003-07-15 23:03:55 +0000401 Py_XDECREF(warnings_module);
402 warnings_module = NULL;
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000403
Christian Heimes908caac2008-01-27 23:34:59 +0000404 /* Clear type lookup cache */
405 PyType_ClearCache();
406
Guido van Rossume13ddc92003-04-17 17:29:22 +0000407 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000408 * before all modules are destroyed.
409 * XXX If a __del__ or weakref callback is triggered here, and tries to
410 * XXX import a module, bad things can happen, because Python no
411 * XXX longer believes it's initialized.
412 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
413 * XXX is easy to provoke that way. I've also seen, e.g.,
414 * XXX Exception exceptions.ImportError: 'No module named sha'
415 * XXX in <function callback at 0x008F5718> ignored
416 * XXX but I'm unclear on exactly how that one happens. In any case,
417 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000418 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000419 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000420#ifdef COUNT_ALLOCS
421 /* With COUNT_ALLOCS, it helps to run GC multiple times:
422 each collection might release some types from the type
423 list, so they become garbage. */
424 while (PyGC_Collect() > 0)
425 /* nothing */;
426#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000427
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000428 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000429 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000430
Guido van Rossume13ddc92003-04-17 17:29:22 +0000431 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000432 * new-style class definitions, for example.
433 * XXX This is disabled because it caused too many problems. If
434 * XXX a __del__ or weakref callback triggers here, Python code has
435 * XXX a hard time running, because even the sys module has been
436 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
437 * XXX One symptom is a sequence of information-free messages
438 * XXX coming from threads (if a __del__ or callback is invoked,
439 * XXX other threads can execute too, and any exception they encounter
440 * XXX triggers a comedy of errors as subsystem after subsystem
441 * XXX fails to find what it *expects* to find in sys to help report
442 * XXX the exception and consequent unexpected failures). I've also
443 * XXX seen segfaults then, after adding print statements to the
444 * XXX Python code getting called.
445 */
446#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000447 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000448#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000449
Guido van Rossum1707aad1997-12-08 23:43:45 +0000450 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
451 _PyImport_Fini();
452
453 /* Debugging stuff */
454#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000455 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000456#endif
457
Tim Peters62e97f02006-03-28 21:44:32 +0000458 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000459
Tim Peters9cf25ce2003-04-17 15:21:01 +0000460#ifdef Py_TRACE_REFS
461 /* Display all objects still alive -- this can invoke arbitrary
462 * __repr__ overrides, so requires a mostly-intact interpreter.
463 * Alas, a lot of stuff may still be alive now that will be cleaned
464 * up later.
465 */
Tim Peters269b2a62003-04-17 19:52:29 +0000466 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000467 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000468#endif /* Py_TRACE_REFS */
469
Guido van Rossumd922fa42003-04-15 14:10:09 +0000470 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000471 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000472
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000473 /* Now we decref the exception classes. After this point nothing
474 can raise an exception. That's okay, because each Fini() method
475 below has been checked to make sure no exceptions are ever
476 raised.
477 */
478
479 _PyExc_Fini();
480
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000481 /* Cleanup auto-thread-state */
482#ifdef WITH_THREAD
483 _PyGILState_Fini();
484#endif /* WITH_THREAD */
485
Guido van Rossumd922fa42003-04-15 14:10:09 +0000486 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000487 PyThreadState_Swap(NULL);
488 PyInterpreterState_Delete(interp);
489
Guido van Rossumd922fa42003-04-15 14:10:09 +0000490 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000491 PyMethod_Fini();
492 PyFrame_Fini();
493 PyCFunction_Fini();
494 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000495 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000496 PySet_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000497 PyString_Fini();
Christian Heimes1a6387e2008-03-26 12:49:49 +0000498 PyBytes_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000499 PyInt_Fini();
500 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000501 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000502
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000503#ifdef Py_USING_UNICODE
504 /* Cleanup Unicode implementation */
505 _PyUnicode_Fini();
506#endif
507
Guido van Rossumcc283f51997-08-05 02:22:03 +0000508 /* XXX Still allocated:
509 - various static ad-hoc pointers to interned strings
510 - int and float free list blocks
511 - whatever various modules and libraries allocate
512 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000513
514 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000515
Tim Peters269b2a62003-04-17 19:52:29 +0000516#ifdef Py_TRACE_REFS
517 /* Display addresses (& refcnts) of all objects still alive.
518 * An address can be used to find the repr of the object, printed
519 * above by _Py_PrintReferences.
520 */
521 if (Py_GETENV("PYTHONDUMPREFS"))
522 _Py_PrintReferenceAddresses(stderr);
523#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000524#ifdef PYMALLOC_DEBUG
525 if (Py_GETENV("PYTHONMALLOCSTATS"))
526 _PyObject_DebugMallocStats();
527#endif
528
Guido van Rossumcc283f51997-08-05 02:22:03 +0000529 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000530}
531
532/* Create and initialize a new interpreter and thread, and return the
533 new thread. This requires that Py_Initialize() has been called
534 first.
535
536 Unsuccessful initialization yields a NULL pointer. Note that *no*
537 exception information is available even in this case -- the
538 exception information is held in the thread, and there is no
539 thread.
540
541 Locking: as above.
542
543*/
544
545PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000546Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000547{
548 PyInterpreterState *interp;
549 PyThreadState *tstate, *save_tstate;
550 PyObject *bimod, *sysmod;
551
552 if (!initialized)
553 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
554
555 interp = PyInterpreterState_New();
556 if (interp == NULL)
557 return NULL;
558
559 tstate = PyThreadState_New(interp);
560 if (tstate == NULL) {
561 PyInterpreterState_Delete(interp);
562 return NULL;
563 }
564
565 save_tstate = PyThreadState_Swap(tstate);
566
567 /* XXX The following is lax in error checking */
568
569 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000570 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000571
572 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
573 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000574 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000575 if (interp->builtins == NULL)
576 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000577 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578 }
579 sysmod = _PyImport_FindExtension("sys", "sys");
580 if (bimod != NULL && sysmod != NULL) {
581 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000582 if (interp->sysdict == NULL)
583 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000584 Py_INCREF(interp->sysdict);
585 PySys_SetPath(Py_GetPath());
586 PyDict_SetItemString(interp->sysdict, "modules",
587 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000588 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000590 if (!Py_NoSiteFlag)
591 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 }
593
594 if (!PyErr_Occurred())
595 return tstate;
596
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000597handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 /* Oops, it didn't work. Undo it all. */
599
600 PyErr_Print();
601 PyThreadState_Clear(tstate);
602 PyThreadState_Swap(save_tstate);
603 PyThreadState_Delete(tstate);
604 PyInterpreterState_Delete(interp);
605
606 return NULL;
607}
608
609/* Delete an interpreter and its last thread. This requires that the
610 given thread state is current, that the thread has no remaining
611 frames, and that it is its interpreter's only remaining thread.
612 It is a fatal error to violate these constraints.
613
614 (Py_Finalize() doesn't have these constraints -- it zaps
615 everything, regardless.)
616
617 Locking: as above.
618
619*/
620
621void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000622Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000623{
624 PyInterpreterState *interp = tstate->interp;
625
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000626 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000627 Py_FatalError("Py_EndInterpreter: thread is not current");
628 if (tstate->frame != NULL)
629 Py_FatalError("Py_EndInterpreter: thread still has a frame");
630 if (tstate != interp->tstate_head || tstate->next != NULL)
631 Py_FatalError("Py_EndInterpreter: not the last thread");
632
633 PyImport_Cleanup();
634 PyInterpreterState_Clear(interp);
635 PyThreadState_Swap(NULL);
636 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000637}
638
639static char *progname = "python";
640
641void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000642Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000643{
644 if (pn && *pn)
645 progname = pn;
646}
647
648char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000650{
651 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000652}
653
Guido van Rossuma61691e1998-02-06 22:27:24 +0000654static char *default_home = NULL;
655
656void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000657Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000658{
659 default_home = home;
660}
661
662char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000663Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000664{
665 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000666 if (home == NULL && !Py_IgnoreEnvironmentFlag)
667 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000668 return home;
669}
670
Guido van Rossum6135a871995-01-09 17:53:26 +0000671/* Create __main__ module */
672
673static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000674initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000675{
Guido van Rossum82598051997-03-05 00:20:32 +0000676 PyObject *m, *d;
677 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000678 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000679 Py_FatalError("can't create __main__ module");
680 d = PyModule_GetDict(m);
681 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000682 PyObject *bimod = PyImport_ImportModule("__builtin__");
683 if (bimod == NULL ||
684 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000685 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000686 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000687 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000688}
689
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000690/* Import the site module (not into __main__ though) */
691
692static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000693initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000694{
695 PyObject *m, *f;
696 m = PyImport_ImportModule("site");
697 if (m == NULL) {
698 f = PySys_GetObject("stderr");
699 if (Py_VerboseFlag) {
700 PyFile_WriteString(
701 "'import site' failed; traceback:\n", f);
702 PyErr_Print();
703 }
704 else {
705 PyFile_WriteString(
706 "'import site' failed; use -v for traceback\n", f);
707 PyErr_Clear();
708 }
709 }
710 else {
711 Py_DECREF(m);
712 }
713}
714
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000715/* Parse input from a file and execute it */
716
717int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000718PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000719 PyCompilerFlags *flags)
720{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000721 if (filename == NULL)
722 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000723 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000724 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000725 if (closeit)
726 fclose(fp);
727 return err;
728 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000729 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000730 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000731}
732
733int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000734PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000735{
Guido van Rossum82598051997-03-05 00:20:32 +0000736 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000737 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000738 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000739
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000740 if (flags == NULL) {
741 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000742 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000743 }
Guido van Rossum82598051997-03-05 00:20:32 +0000744 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000745 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000746 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
747 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000748 }
Guido van Rossum82598051997-03-05 00:20:32 +0000749 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000750 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000751 PySys_SetObject("ps2", v = PyString_FromString("... "));
752 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753 }
754 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000755 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000756 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000757 if (ret == E_EOF)
758 return 0;
759 /*
760 if (ret == E_NOMEM)
761 return -1;
762 */
763 }
764}
765
Eric Smith7c478942008-03-18 23:45:49 +0000766#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000767/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000768#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000769 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000770 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000771#endif
772#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000773/* Keep an example of flags with future keyword support. */
774#define PARSER_FLAGS(flags) \
775 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000776 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000777 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
778 PyPARSE_PRINT_IS_FUNCTION : 0) \
779 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
780 PyPARSE_UNICODE_LITERALS : 0) \
781 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000782#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000783
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000784int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000785PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000786{
Guido van Rossum82598051997-03-05 00:20:32 +0000787 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000789 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000790 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000792
Guido van Rossum82598051997-03-05 00:20:32 +0000793 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000794 if (v != NULL) {
795 v = PyObject_Str(v);
796 if (v == NULL)
797 PyErr_Clear();
798 else if (PyString_Check(v))
799 ps1 = PyString_AsString(v);
800 }
Guido van Rossum82598051997-03-05 00:20:32 +0000801 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000802 if (w != NULL) {
803 w = PyObject_Str(w);
804 if (w == NULL)
805 PyErr_Clear();
806 else if (PyString_Check(w))
807 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000808 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000809 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000810 if (arena == NULL) {
811 Py_XDECREF(v);
812 Py_XDECREF(w);
813 return -1;
814 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000815 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000817 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000818 Py_XDECREF(v);
819 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000821 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (errcode == E_EOF) {
823 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000824 return E_EOF;
825 }
Guido van Rossum82598051997-03-05 00:20:32 +0000826 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000828 }
Guido van Rossum82598051997-03-05 00:20:32 +0000829 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000831 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000832 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000836 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000837 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000838 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000839 return -1;
840 }
Guido van Rossum82598051997-03-05 00:20:32 +0000841 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000842 if (Py_FlushLine())
843 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000844 return 0;
845}
846
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000847/* Check whether a file maybe a pyc file: Look at the extension,
848 the file type, and, if we may close it, at the first few bytes. */
849
850static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000851maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000852{
853 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
854 return 1;
855
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000856 /* Only look into the file if we are allowed to close it, since
857 it then should also be seekable. */
858 if (closeit) {
859 /* Read only two bytes of the magic. If the file was opened in
860 text mode, the bytes 3 and 4 of the magic (\r\n) might not
861 be read as they are on disk. */
862 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
863 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000864 /* Mess: In case of -x, the stream is NOT at its start now,
865 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000866 which makes the current stream position formally undefined,
867 and a x-platform nightmare.
868 Unfortunately, we have no direct way to know whether -x
869 was specified. So we use a terrible hack: if the current
870 stream position is not 0, we assume -x was specified, and
871 give up. Bug 132850 on SourceForge spells out the
872 hopelessness of trying anything else (fseek and ftell
873 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000874 */
Tim Peters3e876562001-02-11 04:35:39 +0000875 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000876 if (ftell(fp) == 0) {
877 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000878 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000879 ispyc = 1;
880 rewind(fp);
881 }
Tim Peters3e876562001-02-11 04:35:39 +0000882 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000883 }
884 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000885}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000886
Guido van Rossum0df002c2000-08-27 19:21:52 +0000887int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000888PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000889 PyCompilerFlags *flags)
890{
Guido van Rossum82598051997-03-05 00:20:32 +0000891 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000892 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000893 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000894
Guido van Rossum82598051997-03-05 00:20:32 +0000895 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000896 if (m == NULL)
897 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000898 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000899 if (PyDict_GetItemString(d, "__file__") == NULL) {
900 PyObject *f = PyString_FromString(filename);
901 if (f == NULL)
902 return -1;
903 if (PyDict_SetItemString(d, "__file__", f) < 0) {
904 Py_DECREF(f);
905 return -1;
906 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000907 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000908 Py_DECREF(f);
909 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000910 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000911 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000912 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000913 if (closeit)
914 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000915 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000916 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000917 ret = -1;
918 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000919 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000920 /* Turn on optimization if a .pyo file is given */
921 if (strcmp(ext, ".pyo") == 0)
922 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000923 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000924 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000925 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000926 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000927 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000928 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000929 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000930 ret = -1;
931 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000932 }
Guido van Rossum82598051997-03-05 00:20:32 +0000933 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000934 if (Py_FlushLine())
935 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000936 ret = 0;
937 done:
938 if (set_file_name && PyDict_DelItemString(d, "__file__"))
939 PyErr_Clear();
940 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000941}
942
943int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000944PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000945{
Guido van Rossum82598051997-03-05 00:20:32 +0000946 PyObject *m, *d, *v;
947 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000948 if (m == NULL)
949 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000950 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000951 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000952 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000953 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954 return -1;
955 }
Guido van Rossum82598051997-03-05 00:20:32 +0000956 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000957 if (Py_FlushLine())
958 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000959 return 0;
960}
961
Barry Warsaw035574d1997-08-29 22:07:17 +0000962static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000963parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
964 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000965{
966 long hold;
967 PyObject *v;
968
969 /* old style errors */
970 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000971 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000972 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000973
974 /* new style errors. `err' is an instance */
975
976 if (! (v = PyObject_GetAttrString(err, "msg")))
977 goto finally;
978 *message = v;
979
980 if (!(v = PyObject_GetAttrString(err, "filename")))
981 goto finally;
982 if (v == Py_None)
983 *filename = NULL;
984 else if (! (*filename = PyString_AsString(v)))
985 goto finally;
986
987 Py_DECREF(v);
988 if (!(v = PyObject_GetAttrString(err, "lineno")))
989 goto finally;
990 hold = PyInt_AsLong(v);
991 Py_DECREF(v);
992 v = NULL;
993 if (hold < 0 && PyErr_Occurred())
994 goto finally;
995 *lineno = (int)hold;
996
997 if (!(v = PyObject_GetAttrString(err, "offset")))
998 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000999 if (v == Py_None) {
1000 *offset = -1;
1001 Py_DECREF(v);
1002 v = NULL;
1003 } else {
1004 hold = PyInt_AsLong(v);
1005 Py_DECREF(v);
1006 v = NULL;
1007 if (hold < 0 && PyErr_Occurred())
1008 goto finally;
1009 *offset = (int)hold;
1010 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001011
1012 if (!(v = PyObject_GetAttrString(err, "text")))
1013 goto finally;
1014 if (v == Py_None)
1015 *text = NULL;
1016 else if (! (*text = PyString_AsString(v)))
1017 goto finally;
1018 Py_DECREF(v);
1019 return 1;
1020
1021finally:
1022 Py_XDECREF(v);
1023 return 0;
1024}
1025
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001026void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001027PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001028{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001029 PyErr_PrintEx(1);
1030}
1031
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001032static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001033print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001034{
1035 char *nl;
1036 if (offset >= 0) {
1037 if (offset > 0 && offset == (int)strlen(text))
1038 offset--;
1039 for (;;) {
1040 nl = strchr(text, '\n');
1041 if (nl == NULL || nl-text >= offset)
1042 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001043 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001044 text = nl+1;
1045 }
1046 while (*text == ' ' || *text == '\t') {
1047 text++;
1048 offset--;
1049 }
1050 }
1051 PyFile_WriteString(" ", f);
1052 PyFile_WriteString(text, f);
1053 if (*text == '\0' || text[strlen(text)-1] != '\n')
1054 PyFile_WriteString("\n", f);
1055 if (offset == -1)
1056 return;
1057 PyFile_WriteString(" ", f);
1058 offset--;
1059 while (offset > 0) {
1060 PyFile_WriteString(" ", f);
1061 offset--;
1062 }
1063 PyFile_WriteString("^\n", f);
1064}
1065
Guido van Rossum66e8e862001-03-23 17:54:43 +00001066static void
1067handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001068{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001069 PyObject *exception, *value, *tb;
1070 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001071
Georg Brandl49aafc92007-03-07 00:34:46 +00001072 if (Py_InspectFlag)
1073 /* Don't exit if -i flag was given. This flag is set to 0
1074 * when entering interactive mode for inspecting. */
1075 return;
1076
Guido van Rossum66e8e862001-03-23 17:54:43 +00001077 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001078 if (Py_FlushLine())
1079 PyErr_Clear();
1080 fflush(stdout);
1081 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001082 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001083 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001084 /* The error code should be in the `code' attribute. */
1085 PyObject *code = PyObject_GetAttrString(value, "code");
1086 if (code) {
1087 Py_DECREF(value);
1088 value = code;
1089 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001090 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001091 }
1092 /* If we failed to dig out the 'code' attribute,
1093 just let the else clause below print the error. */
1094 }
1095 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001096 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001097 else {
1098 PyObject_Print(value, stderr, Py_PRINT_RAW);
1099 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001100 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001101 }
Tim Peterscf615b52003-04-19 18:47:02 +00001102 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001103 /* Restore and clear the exception info, in order to properly decref
1104 * the exception, value, and traceback. If we just exit instead,
1105 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1106 * some finalizers from running.
1107 */
Tim Peterscf615b52003-04-19 18:47:02 +00001108 PyErr_Restore(exception, value, tb);
1109 PyErr_Clear();
1110 Py_Exit(exitcode);
1111 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001112}
1113
1114void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001115PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001116{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001117 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001118
1119 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1120 handle_system_exit();
1121 }
Guido van Rossum82598051997-03-05 00:20:32 +00001122 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 if (exception == NULL)
1124 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001125 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001126 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001127 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001128 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001129 if (set_sys_last_vars) {
1130 PySys_SetObject("last_type", exception);
1131 PySys_SetObject("last_value", v);
1132 PySys_SetObject("last_traceback", tb);
1133 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001134 hook = PySys_GetObject("excepthook");
1135 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001136 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001137 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001138 PyObject *result = PyEval_CallObject(hook, args);
1139 if (result == NULL) {
1140 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001141 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1142 handle_system_exit();
1143 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001144 PyErr_Fetch(&exception2, &v2, &tb2);
1145 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001146 /* It should not be possible for exception2 or v2
1147 to be NULL. However PyErr_Display() can't
1148 tolerate NULLs, so just be safe. */
1149 if (exception2 == NULL) {
1150 exception2 = Py_None;
1151 Py_INCREF(exception2);
1152 }
1153 if (v2 == NULL) {
1154 v2 = Py_None;
1155 Py_INCREF(v2);
1156 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001157 if (Py_FlushLine())
1158 PyErr_Clear();
1159 fflush(stdout);
1160 PySys_WriteStderr("Error in sys.excepthook:\n");
1161 PyErr_Display(exception2, v2, tb2);
1162 PySys_WriteStderr("\nOriginal exception was:\n");
1163 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001164 Py_DECREF(exception2);
1165 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001166 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001167 }
1168 Py_XDECREF(result);
1169 Py_XDECREF(args);
1170 } else {
1171 PySys_WriteStderr("sys.excepthook is missing\n");
1172 PyErr_Display(exception, v, tb);
1173 }
1174 Py_XDECREF(exception);
1175 Py_XDECREF(v);
1176 Py_XDECREF(tb);
1177}
1178
Richard Jones7b9558d2006-05-27 12:29:24 +00001179void
1180PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001181{
1182 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001183 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001184 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001185 if (f == NULL)
1186 fprintf(stderr, "lost sys.stderr\n");
1187 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001188 if (Py_FlushLine())
1189 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001190 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001191 if (tb && tb != Py_None)
1192 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001193 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001194 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001195 {
Guido van Rossum82598051997-03-05 00:20:32 +00001196 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001197 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001198 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001199 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001200 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001201 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001202 else {
1203 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001204 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001205 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001206 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001207 else
Guido van Rossum82598051997-03-05 00:20:32 +00001208 PyFile_WriteString(filename, f);
1209 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001210 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001211 PyFile_WriteString(buf, f);
1212 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001213 if (text != NULL)
1214 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001215 Py_DECREF(value);
1216 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001217 /* Can't be bothered to check all those
1218 PyFile_WriteString() calls */
1219 if (PyErr_Occurred())
1220 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001221 }
1222 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001223 if (err) {
1224 /* Don't do anything else */
1225 }
Brett Cannonbf364092006-03-01 04:25:17 +00001226 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001227 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001228 char* className = PyExceptionClass_Name(exception);
1229 if (className != NULL) {
1230 char *dot = strrchr(className, '.');
1231 if (dot != NULL)
1232 className = dot+1;
1233 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001234
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001235 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001236 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001237 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001238 else {
1239 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001240 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001241 {
1242 err = PyFile_WriteString(modstr, f);
1243 err += PyFile_WriteString(".", f);
1244 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001245 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001246 }
1247 if (err == 0) {
1248 if (className == NULL)
1249 err = PyFile_WriteString("<unknown>", f);
1250 else
Brett Cannonbf364092006-03-01 04:25:17 +00001251 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001252 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001253 }
1254 else
1255 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001256 if (err == 0 && (value != Py_None)) {
1257 PyObject *s = PyObject_Str(value);
1258 /* only print colon if the str() of the
1259 object is not the empty string
1260 */
1261 if (s == NULL)
1262 err = -1;
1263 else if (!PyString_Check(s) ||
1264 PyString_GET_SIZE(s) != 0)
1265 err = PyFile_WriteString(": ", f);
1266 if (err == 0)
1267 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1268 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001269 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001270 /* try to write a newline in any case */
1271 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001272 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001273 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001274 /* If an error happened here, don't show it.
1275 XXX This is wrong, but too many callers rely on this behavior. */
1276 if (err != 0)
1277 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001278}
1279
Guido van Rossum82598051997-03-05 00:20:32 +00001280PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001281PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001283{
Neal Norwitze92fba02006-03-04 18:52:26 +00001284 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001285 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001286 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001287 if (arena == NULL)
1288 return NULL;
1289
1290 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001291 if (mod != NULL)
1292 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001293 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001295}
1296
1297PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001298PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001299 PyObject *locals, int closeit, PyCompilerFlags *flags)
1300{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001302 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001303 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001304 if (arena == NULL)
1305 return NULL;
1306
1307 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1308 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001309 if (closeit)
1310 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001311 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001312 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001314 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001315 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001316 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001318}
1319
Guido van Rossum82598051997-03-05 00:20:32 +00001320static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001323{
Guido van Rossum82598051997-03-05 00:20:32 +00001324 PyCodeObject *co;
1325 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001326 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001327 if (co == NULL)
1328 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001329 v = PyEval_EvalCode(co, globals, locals);
1330 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001331 return v;
1332}
1333
Guido van Rossum82598051997-03-05 00:20:32 +00001334static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001335run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001337{
Guido van Rossum82598051997-03-05 00:20:32 +00001338 PyCodeObject *co;
1339 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001340 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001341 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001342
Guido van Rossum82598051997-03-05 00:20:32 +00001343 magic = PyMarshal_ReadLongFromFile(fp);
1344 if (magic != PyImport_GetMagicNumber()) {
1345 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001346 "Bad magic number in .pyc file");
1347 return NULL;
1348 }
Guido van Rossum82598051997-03-05 00:20:32 +00001349 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001350 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001351 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001352 if (v == NULL || !PyCode_Check(v)) {
1353 Py_XDECREF(v);
1354 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001355 "Bad code object in .pyc file");
1356 return NULL;
1357 }
Guido van Rossum82598051997-03-05 00:20:32 +00001358 co = (PyCodeObject *)v;
1359 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001360 if (v && flags)
1361 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001362 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001363 return v;
1364}
1365
Guido van Rossum82598051997-03-05 00:20:32 +00001366PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001367Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001368 PyCompilerFlags *flags)
1369{
Guido van Rossum82598051997-03-05 00:20:32 +00001370 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001371 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001372 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001373 if (arena == NULL)
1374 return NULL;
1375
1376 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001378 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001379 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001380 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001381 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001382 PyObject *result = PyAST_mod2obj(mod);
1383 PyArena_Free(arena);
1384 return result;
1385 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001386 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001387 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001388 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001389}
1390
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001391struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001392Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001393{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001394 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001395 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001396 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001397 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001398 if (arena == NULL)
1399 return NULL;
1400
Christian Heimes7f23d862008-03-26 22:51:58 +00001401 flags.cf_flags = 0;
1402
Christian Heimes3c608332008-03-26 22:01:37 +00001403 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001404 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001405 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001406 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001407 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001409 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001410 return st;
1411}
1412
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413/* Preferred access to parser is through AST. */
1414mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001415PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 mod_ty mod;
1419 perrdetail err;
Christian Heimes3c608332008-03-26 22:01:37 +00001420 int iflags;
1421 iflags = PARSER_FLAGS(flags);
1422
1423 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001424 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001425 &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001427 if (flags) {
1428 flags->cf_flags |= iflags & PyCF_MASK;
1429 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001430 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 PyNode_Free(n);
1432 return mod;
1433 }
1434 else {
1435 err_input(&err);
1436 return NULL;
1437 }
1438}
1439
1440mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001441PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001443 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 mod_ty mod;
1446 perrdetail err;
Christian Heimes3c608332008-03-26 22:01:37 +00001447 int iflags;
1448
1449 iflags = PARSER_FLAGS(flags);
1450 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1451 start, ps1, ps2, &err, &iflags);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 if (n) {
Christian Heimes3c608332008-03-26 22:01:37 +00001453 if (flags) {
1454 flags->cf_flags |= iflags & PyCF_MASK;
1455 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001456 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 PyNode_Free(n);
1458 return mod;
1459 }
1460 else {
1461 err_input(&err);
1462 if (errcode)
1463 *errcode = err.error;
1464 return NULL;
1465 }
1466}
1467
Guido van Rossuma110aa61994-08-29 12:50:44 +00001468/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001469
Guido van Rossuma110aa61994-08-29 12:50:44 +00001470node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001471PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001472{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001473 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001474 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1475 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001476 if (n == NULL)
1477 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001478
Guido van Rossuma110aa61994-08-29 12:50:44 +00001479 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001480}
1481
Guido van Rossuma110aa61994-08-29 12:50:44 +00001482/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001483
Guido van Rossuma110aa61994-08-29 12:50:44 +00001484node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001485PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001486{
Tim Petersfe2127d2001-07-16 05:37:24 +00001487 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1489 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001490 if (n == NULL)
1491 err_input(&err);
1492 return n;
1493}
1494
1495node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001496PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001497 int start, int flags)
1498{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001499 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001500 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1501 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001502 if (n == NULL)
1503 err_input(&err);
1504 return n;
1505}
1506
1507node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001508PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001509{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001510 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001511}
1512
Guido van Rossum66ebd912003-04-17 16:02:26 +00001513/* May want to move a more generalized form of this to parsetok.c or
1514 even parser modules. */
1515
1516void
1517PyParser_SetError(perrdetail *err)
1518{
1519 err_input(err);
1520}
1521
Guido van Rossuma110aa61994-08-29 12:50:44 +00001522/* Set the error appropriate to the given input error code (see errcode.h) */
1523
1524static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001525err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001526{
Fred Drake85f36392000-07-11 17:53:00 +00001527 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001528 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001529 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001530 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001531 switch (err->error) {
1532 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001533 errtype = PyExc_IndentationError;
1534 if (err->expected == INDENT)
1535 msg = "expected an indented block";
1536 else if (err->token == INDENT)
1537 msg = "unexpected indent";
1538 else if (err->token == DEDENT)
1539 msg = "unexpected unindent";
1540 else {
1541 errtype = PyExc_SyntaxError;
1542 msg = "invalid syntax";
1543 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001544 break;
1545 case E_TOKEN:
1546 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001547 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001548 case E_EOFS:
1549 msg = "EOF while scanning triple-quoted string";
1550 break;
1551 case E_EOLS:
1552 msg = "EOL while scanning single-quoted string";
1553 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001554 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001555 if (!PyErr_Occurred())
1556 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001557 return;
1558 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001559 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001560 return;
1561 case E_EOF:
1562 msg = "unexpected EOF while parsing";
1563 break;
Fred Drake85f36392000-07-11 17:53:00 +00001564 case E_TABSPACE:
1565 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001566 msg = "inconsistent use of tabs and spaces in indentation";
1567 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001568 case E_OVERFLOW:
1569 msg = "expression too long";
1570 break;
Fred Drake85f36392000-07-11 17:53:00 +00001571 case E_DEDENT:
1572 errtype = PyExc_IndentationError;
1573 msg = "unindent does not match any outer indentation level";
1574 break;
1575 case E_TOODEEP:
1576 errtype = PyExc_IndentationError;
1577 msg = "too many levels of indentation";
1578 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001579 case E_DECODE: {
1580 PyObject *type, *value, *tb;
1581 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001582 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001583 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001584 if (u != NULL) {
1585 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001586 }
1587 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001588 if (msg == NULL)
1589 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001590 Py_XDECREF(type);
1591 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001592 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001593 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001594 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001595 case E_LINECONT:
1596 msg = "unexpected character after line continuation character";
1597 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001598 default:
1599 fprintf(stderr, "error=%d\n", err->error);
1600 msg = "unknown parsing error";
1601 break;
1602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 v = Py_BuildValue("(ziiz)", err->filename,
1604 err->lineno, err->offset, err->text);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001605 if (err->text != NULL) {
Tim Petersc9d78aa2006-03-26 23:27:58 +00001606 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 err->text = NULL;
1608 }
1609 w = NULL;
1610 if (v != NULL)
1611 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001612 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001613 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001614 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001615 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001616}
1617
1618/* Print fatal error message and abort */
1619
1620void
Tim Peters7c321a82002-07-09 02:57:01 +00001621Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001622{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001623 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001624#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001625 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001626 OutputDebugString(msg);
1627 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001628#ifdef _DEBUG
1629 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001630#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001631#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001632 abort();
1633}
1634
1635/* Clean up and exit */
1636
Guido van Rossuma110aa61994-08-29 12:50:44 +00001637#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001638#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001639#endif
1640
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001641#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001642static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001643static int nexitfuncs = 0;
1644
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001645int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001646{
1647 if (nexitfuncs >= NEXITFUNCS)
1648 return -1;
1649 exitfuncs[nexitfuncs++] = func;
1650 return 0;
1651}
1652
Guido van Rossumcc283f51997-08-05 02:22:03 +00001653static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001654call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001655{
Guido van Rossum82598051997-03-05 00:20:32 +00001656 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001657
1658 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001659 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001660 Py_INCREF(exitfunc);
1661 PySys_SetObject("exitfunc", (PyObject *)NULL);
1662 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001663 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001664 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1665 PySys_WriteStderr("Error in sys.exitfunc:\n");
1666 }
Guido van Rossum82598051997-03-05 00:20:32 +00001667 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001668 }
Guido van Rossum82598051997-03-05 00:20:32 +00001669 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001670 }
1671
Guido van Rossum0829c751998-02-28 04:31:39 +00001672 if (Py_FlushLine())
1673 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001674}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001675
Guido van Rossumcc283f51997-08-05 02:22:03 +00001676static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001677call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001678{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001679 while (nexitfuncs > 0)
1680 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001681
1682 fflush(stdout);
1683 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001684}
1685
1686void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001687Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001688{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001689 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001690
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001691 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001692}
1693
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001694static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001695initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001696{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001697#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001698 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001699#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001700#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001701 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001702#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001703#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001704 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001705#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001706 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001707}
1708
Guido van Rossum7433b121997-02-14 19:45:36 +00001709
1710/*
1711 * The file descriptor fd is considered ``interactive'' if either
1712 * a) isatty(fd) is TRUE, or
1713 * b) the -i flag was given, and the filename associated with
1714 * the descriptor is NULL or "<stdin>" or "???".
1715 */
1716int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001717Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001718{
1719 if (isatty((int)fileno(fp)))
1720 return 1;
1721 if (!Py_InteractiveFlag)
1722 return 0;
1723 return (filename == NULL) ||
1724 (strcmp(filename, "<stdin>") == 0) ||
1725 (strcmp(filename, "???") == 0);
1726}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001727
1728
Tim Petersd08e3822003-04-17 15:24:21 +00001729#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001730#if defined(WIN32) && defined(_MSC_VER)
1731
1732/* Stack checking for Microsoft C */
1733
1734#include <malloc.h>
1735#include <excpt.h>
1736
Fred Drakee8de31c2000-08-31 05:38:39 +00001737/*
1738 * Return non-zero when we run out of memory on the stack; zero otherwise.
1739 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001740int
Fred Drake399739f2000-08-31 05:52:44 +00001741PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001742{
1743 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001744 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001745 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001746 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001747 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001748 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1749 EXCEPTION_EXECUTE_HANDLER :
1750 EXCEPTION_CONTINUE_SEARCH) {
1751 int errcode = _resetstkoflw();
1752 if (errcode)
1753 {
1754 Py_FatalError("Could not reset the stack!");
1755 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001756 }
1757 return 1;
1758}
1759
1760#endif /* WIN32 && _MSC_VER */
1761
1762/* Alternate implementations can be added here... */
1763
1764#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001765
1766
1767/* Wrappers around sigaction() or signal(). */
1768
1769PyOS_sighandler_t
1770PyOS_getsig(int sig)
1771{
1772#ifdef HAVE_SIGACTION
1773 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001774 if (sigaction(sig, NULL, &context) == -1)
1775 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001776 return context.sa_handler;
1777#else
1778 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001779/* Special signal handling for the secure CRT in Visual Studio 2005 */
1780#if defined(_MSC_VER) && _MSC_VER >= 1400
1781 switch (sig) {
1782 /* Only these signals are valid */
1783 case SIGINT:
1784 case SIGILL:
1785 case SIGFPE:
1786 case SIGSEGV:
1787 case SIGTERM:
1788 case SIGBREAK:
1789 case SIGABRT:
1790 break;
1791 /* Don't call signal() with other values or it will assert */
1792 default:
1793 return SIG_ERR;
1794 }
1795#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001796 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001797 if (handler != SIG_ERR)
1798 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001799 return handler;
1800#endif
1801}
1802
1803PyOS_sighandler_t
1804PyOS_setsig(int sig, PyOS_sighandler_t handler)
1805{
1806#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001807 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001808 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001809 sigemptyset(&context.sa_mask);
1810 context.sa_flags = 0;
1811 if (sigaction(sig, &context, &ocontext) == -1)
1812 return SIG_ERR;
1813 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001814#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001815 PyOS_sighandler_t oldhandler;
1816 oldhandler = signal(sig, handler);
1817#ifdef HAVE_SIGINTERRUPT
1818 siginterrupt(sig, 1);
1819#endif
1820 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001821#endif
1822}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823
1824/* Deprecated C API functions still provided for binary compatiblity */
1825
1826#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001827PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1829{
1830 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1831}
1832
Thomas Heller1b046642006-04-18 18:51:06 +00001833#undef PyParser_SimpleParseString
1834PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835PyParser_SimpleParseString(const char *str, int start)
1836{
1837 return PyParser_SimpleParseStringFlags(str, start, 0);
1838}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001839
Thomas Heller1b046642006-04-18 18:51:06 +00001840#undef PyRun_AnyFile
1841PyAPI_FUNC(int)
1842PyRun_AnyFile(FILE *fp, const char *name)
1843{
1844 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1845}
1846
1847#undef PyRun_AnyFileEx
1848PyAPI_FUNC(int)
1849PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1850{
1851 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1852}
1853
1854#undef PyRun_AnyFileFlags
1855PyAPI_FUNC(int)
1856PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1857{
1858 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1859}
1860
1861#undef PyRun_File
1862PyAPI_FUNC(PyObject *)
1863PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1864{
1865 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1866}
1867
1868#undef PyRun_FileEx
1869PyAPI_FUNC(PyObject *)
1870PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1871{
1872 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1873}
1874
1875#undef PyRun_FileFlags
1876PyAPI_FUNC(PyObject *)
1877PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1878 PyCompilerFlags *flags)
1879{
1880 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1881}
1882
1883#undef PyRun_SimpleFile
1884PyAPI_FUNC(int)
1885PyRun_SimpleFile(FILE *f, const char *p)
1886{
1887 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1888}
1889
1890#undef PyRun_SimpleFileEx
1891PyAPI_FUNC(int)
1892PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1893{
1894 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1895}
1896
1897
1898#undef PyRun_String
1899PyAPI_FUNC(PyObject *)
1900PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1901{
1902 return PyRun_StringFlags(str, s, g, l, NULL);
1903}
1904
1905#undef PyRun_SimpleString
1906PyAPI_FUNC(int)
1907PyRun_SimpleString(const char *s)
1908{
1909 return PyRun_SimpleStringFlags(s, NULL);
1910}
1911
1912#undef Py_CompileString
1913PyAPI_FUNC(PyObject *)
1914Py_CompileString(const char *str, const char *p, int s)
1915{
1916 return Py_CompileStringFlags(str, p, s, NULL);
1917}
1918
1919#undef PyRun_InteractiveOne
1920PyAPI_FUNC(int)
1921PyRun_InteractiveOne(FILE *f, const char *p)
1922{
1923 return PyRun_InteractiveOneFlags(f, p, NULL);
1924}
1925
1926#undef PyRun_InteractiveLoop
1927PyAPI_FUNC(int)
1928PyRun_InteractiveLoop(FILE *f, const char *p)
1929{
1930 return PyRun_InteractiveLoopFlags(f, p, NULL);
1931}
1932
Anthony Baxterac6bd462006-04-13 02:06:09 +00001933#ifdef __cplusplus
1934}
1935#endif
1936