blob: 226fee393010ac83d9c7052738105ae31ea453c2 [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) \
Eric Smith7c478942008-03-18 23:45:49 +0000777 | ((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION ? \
778 PyPARSE_PRINT_IS_FUNCTION : 0)) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000779#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000780
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000781int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000782PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000783{
Guido van Rossum82598051997-03-05 00:20:32 +0000784 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000786 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000787 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000789
Guido van Rossum82598051997-03-05 00:20:32 +0000790 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000791 if (v != NULL) {
792 v = PyObject_Str(v);
793 if (v == NULL)
794 PyErr_Clear();
795 else if (PyString_Check(v))
796 ps1 = PyString_AsString(v);
797 }
Guido van Rossum82598051997-03-05 00:20:32 +0000798 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000799 if (w != NULL) {
800 w = PyObject_Str(w);
801 if (w == NULL)
802 PyErr_Clear();
803 else if (PyString_Check(w))
804 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000805 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000806 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000807 if (arena == NULL) {
808 Py_XDECREF(v);
809 Py_XDECREF(w);
810 return -1;
811 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000812 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000815 Py_XDECREF(v);
816 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000818 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 if (errcode == E_EOF) {
820 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000821 return E_EOF;
822 }
Guido van Rossum82598051997-03-05 00:20:32 +0000823 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000825 }
Guido van Rossum82598051997-03-05 00:20:32 +0000826 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000827 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000828 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000829 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000830 }
Guido van Rossum82598051997-03-05 00:20:32 +0000831 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000832 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000833 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000834 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000835 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000836 return -1;
837 }
Guido van Rossum82598051997-03-05 00:20:32 +0000838 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000839 if (Py_FlushLine())
840 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841 return 0;
842}
843
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000844/* Check whether a file maybe a pyc file: Look at the extension,
845 the file type, and, if we may close it, at the first few bytes. */
846
847static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000848maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000849{
850 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
851 return 1;
852
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000853 /* Only look into the file if we are allowed to close it, since
854 it then should also be seekable. */
855 if (closeit) {
856 /* Read only two bytes of the magic. If the file was opened in
857 text mode, the bytes 3 and 4 of the magic (\r\n) might not
858 be read as they are on disk. */
859 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
860 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000861 /* Mess: In case of -x, the stream is NOT at its start now,
862 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000863 which makes the current stream position formally undefined,
864 and a x-platform nightmare.
865 Unfortunately, we have no direct way to know whether -x
866 was specified. So we use a terrible hack: if the current
867 stream position is not 0, we assume -x was specified, and
868 give up. Bug 132850 on SourceForge spells out the
869 hopelessness of trying anything else (fseek and ftell
870 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000871 */
Tim Peters3e876562001-02-11 04:35:39 +0000872 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000873 if (ftell(fp) == 0) {
874 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000875 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000876 ispyc = 1;
877 rewind(fp);
878 }
Tim Peters3e876562001-02-11 04:35:39 +0000879 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000880 }
881 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000882}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000883
Guido van Rossum0df002c2000-08-27 19:21:52 +0000884int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000885PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000886 PyCompilerFlags *flags)
887{
Guido van Rossum82598051997-03-05 00:20:32 +0000888 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000889 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000890 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000891
Guido van Rossum82598051997-03-05 00:20:32 +0000892 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000893 if (m == NULL)
894 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000895 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000896 if (PyDict_GetItemString(d, "__file__") == NULL) {
897 PyObject *f = PyString_FromString(filename);
898 if (f == NULL)
899 return -1;
900 if (PyDict_SetItemString(d, "__file__", f) < 0) {
901 Py_DECREF(f);
902 return -1;
903 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000904 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000905 Py_DECREF(f);
906 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000907 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000908 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000909 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000910 if (closeit)
911 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000912 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000913 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000914 ret = -1;
915 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000916 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000917 /* Turn on optimization if a .pyo file is given */
918 if (strcmp(ext, ".pyo") == 0)
919 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000920 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000921 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000922 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000923 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000924 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000925 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000926 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000927 ret = -1;
928 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000929 }
Guido van Rossum82598051997-03-05 00:20:32 +0000930 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000931 if (Py_FlushLine())
932 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000933 ret = 0;
934 done:
935 if (set_file_name && PyDict_DelItemString(d, "__file__"))
936 PyErr_Clear();
937 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000938}
939
940int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000941PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000942{
Guido van Rossum82598051997-03-05 00:20:32 +0000943 PyObject *m, *d, *v;
944 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000945 if (m == NULL)
946 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000947 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000948 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000949 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000950 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000951 return -1;
952 }
Guido van Rossum82598051997-03-05 00:20:32 +0000953 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000954 if (Py_FlushLine())
955 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000956 return 0;
957}
958
Barry Warsaw035574d1997-08-29 22:07:17 +0000959static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000960parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
961 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000962{
963 long hold;
964 PyObject *v;
965
966 /* old style errors */
967 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000968 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000969 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000970
971 /* new style errors. `err' is an instance */
972
973 if (! (v = PyObject_GetAttrString(err, "msg")))
974 goto finally;
975 *message = v;
976
977 if (!(v = PyObject_GetAttrString(err, "filename")))
978 goto finally;
979 if (v == Py_None)
980 *filename = NULL;
981 else if (! (*filename = PyString_AsString(v)))
982 goto finally;
983
984 Py_DECREF(v);
985 if (!(v = PyObject_GetAttrString(err, "lineno")))
986 goto finally;
987 hold = PyInt_AsLong(v);
988 Py_DECREF(v);
989 v = NULL;
990 if (hold < 0 && PyErr_Occurred())
991 goto finally;
992 *lineno = (int)hold;
993
994 if (!(v = PyObject_GetAttrString(err, "offset")))
995 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000996 if (v == Py_None) {
997 *offset = -1;
998 Py_DECREF(v);
999 v = NULL;
1000 } else {
1001 hold = PyInt_AsLong(v);
1002 Py_DECREF(v);
1003 v = NULL;
1004 if (hold < 0 && PyErr_Occurred())
1005 goto finally;
1006 *offset = (int)hold;
1007 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001008
1009 if (!(v = PyObject_GetAttrString(err, "text")))
1010 goto finally;
1011 if (v == Py_None)
1012 *text = NULL;
1013 else if (! (*text = PyString_AsString(v)))
1014 goto finally;
1015 Py_DECREF(v);
1016 return 1;
1017
1018finally:
1019 Py_XDECREF(v);
1020 return 0;
1021}
1022
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001023void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001024PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001025{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001026 PyErr_PrintEx(1);
1027}
1028
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001029static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001030print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001031{
1032 char *nl;
1033 if (offset >= 0) {
1034 if (offset > 0 && offset == (int)strlen(text))
1035 offset--;
1036 for (;;) {
1037 nl = strchr(text, '\n');
1038 if (nl == NULL || nl-text >= offset)
1039 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001041 text = nl+1;
1042 }
1043 while (*text == ' ' || *text == '\t') {
1044 text++;
1045 offset--;
1046 }
1047 }
1048 PyFile_WriteString(" ", f);
1049 PyFile_WriteString(text, f);
1050 if (*text == '\0' || text[strlen(text)-1] != '\n')
1051 PyFile_WriteString("\n", f);
1052 if (offset == -1)
1053 return;
1054 PyFile_WriteString(" ", f);
1055 offset--;
1056 while (offset > 0) {
1057 PyFile_WriteString(" ", f);
1058 offset--;
1059 }
1060 PyFile_WriteString("^\n", f);
1061}
1062
Guido van Rossum66e8e862001-03-23 17:54:43 +00001063static void
1064handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001065{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001066 PyObject *exception, *value, *tb;
1067 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001068
Georg Brandl49aafc92007-03-07 00:34:46 +00001069 if (Py_InspectFlag)
1070 /* Don't exit if -i flag was given. This flag is set to 0
1071 * when entering interactive mode for inspecting. */
1072 return;
1073
Guido van Rossum66e8e862001-03-23 17:54:43 +00001074 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001075 if (Py_FlushLine())
1076 PyErr_Clear();
1077 fflush(stdout);
1078 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001079 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001080 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001081 /* The error code should be in the `code' attribute. */
1082 PyObject *code = PyObject_GetAttrString(value, "code");
1083 if (code) {
1084 Py_DECREF(value);
1085 value = code;
1086 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001087 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001088 }
1089 /* If we failed to dig out the 'code' attribute,
1090 just let the else clause below print the error. */
1091 }
1092 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001093 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001094 else {
1095 PyObject_Print(value, stderr, Py_PRINT_RAW);
1096 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001097 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001098 }
Tim Peterscf615b52003-04-19 18:47:02 +00001099 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001100 /* Restore and clear the exception info, in order to properly decref
1101 * the exception, value, and traceback. If we just exit instead,
1102 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1103 * some finalizers from running.
1104 */
Tim Peterscf615b52003-04-19 18:47:02 +00001105 PyErr_Restore(exception, value, tb);
1106 PyErr_Clear();
1107 Py_Exit(exitcode);
1108 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001109}
1110
1111void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001112PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001113{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001114 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001115
1116 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1117 handle_system_exit();
1118 }
Guido van Rossum82598051997-03-05 00:20:32 +00001119 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 if (exception == NULL)
1121 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001122 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001123 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001124 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001125 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001126 if (set_sys_last_vars) {
1127 PySys_SetObject("last_type", exception);
1128 PySys_SetObject("last_value", v);
1129 PySys_SetObject("last_traceback", tb);
1130 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001131 hook = PySys_GetObject("excepthook");
1132 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001133 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001134 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001135 PyObject *result = PyEval_CallObject(hook, args);
1136 if (result == NULL) {
1137 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001138 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1139 handle_system_exit();
1140 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001141 PyErr_Fetch(&exception2, &v2, &tb2);
1142 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001143 /* It should not be possible for exception2 or v2
1144 to be NULL. However PyErr_Display() can't
1145 tolerate NULLs, so just be safe. */
1146 if (exception2 == NULL) {
1147 exception2 = Py_None;
1148 Py_INCREF(exception2);
1149 }
1150 if (v2 == NULL) {
1151 v2 = Py_None;
1152 Py_INCREF(v2);
1153 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001154 if (Py_FlushLine())
1155 PyErr_Clear();
1156 fflush(stdout);
1157 PySys_WriteStderr("Error in sys.excepthook:\n");
1158 PyErr_Display(exception2, v2, tb2);
1159 PySys_WriteStderr("\nOriginal exception was:\n");
1160 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001161 Py_DECREF(exception2);
1162 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001163 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001164 }
1165 Py_XDECREF(result);
1166 Py_XDECREF(args);
1167 } else {
1168 PySys_WriteStderr("sys.excepthook is missing\n");
1169 PyErr_Display(exception, v, tb);
1170 }
1171 Py_XDECREF(exception);
1172 Py_XDECREF(v);
1173 Py_XDECREF(tb);
1174}
1175
Richard Jones7b9558d2006-05-27 12:29:24 +00001176void
1177PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001178{
1179 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001180 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001181 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001182 if (f == NULL)
1183 fprintf(stderr, "lost sys.stderr\n");
1184 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001185 if (Py_FlushLine())
1186 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001187 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001188 if (tb && tb != Py_None)
1189 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001190 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001191 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001192 {
Guido van Rossum82598051997-03-05 00:20:32 +00001193 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001194 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001195 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001196 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001197 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001198 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001199 else {
1200 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001201 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001202 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001203 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001204 else
Guido van Rossum82598051997-03-05 00:20:32 +00001205 PyFile_WriteString(filename, f);
1206 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001207 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001208 PyFile_WriteString(buf, f);
1209 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001210 if (text != NULL)
1211 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001212 Py_DECREF(value);
1213 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001214 /* Can't be bothered to check all those
1215 PyFile_WriteString() calls */
1216 if (PyErr_Occurred())
1217 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001218 }
1219 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001220 if (err) {
1221 /* Don't do anything else */
1222 }
Brett Cannonbf364092006-03-01 04:25:17 +00001223 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001224 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001225 char* className = PyExceptionClass_Name(exception);
1226 if (className != NULL) {
1227 char *dot = strrchr(className, '.');
1228 if (dot != NULL)
1229 className = dot+1;
1230 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001231
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001232 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001233 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001234 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001235 else {
1236 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001237 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001238 {
1239 err = PyFile_WriteString(modstr, f);
1240 err += PyFile_WriteString(".", f);
1241 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001242 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001243 }
1244 if (err == 0) {
1245 if (className == NULL)
1246 err = PyFile_WriteString("<unknown>", f);
1247 else
Brett Cannonbf364092006-03-01 04:25:17 +00001248 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001249 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001250 }
1251 else
1252 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001253 if (err == 0 && (value != Py_None)) {
1254 PyObject *s = PyObject_Str(value);
1255 /* only print colon if the str() of the
1256 object is not the empty string
1257 */
1258 if (s == NULL)
1259 err = -1;
1260 else if (!PyString_Check(s) ||
1261 PyString_GET_SIZE(s) != 0)
1262 err = PyFile_WriteString(": ", f);
1263 if (err == 0)
1264 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1265 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001266 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001267 /* try to write a newline in any case */
1268 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001269 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001270 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001271 /* If an error happened here, don't show it.
1272 XXX This is wrong, but too many callers rely on this behavior. */
1273 if (err != 0)
1274 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001275}
1276
Guido van Rossum82598051997-03-05 00:20:32 +00001277PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001278PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001280{
Neal Norwitze92fba02006-03-04 18:52:26 +00001281 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001282 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001283 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001284 if (arena == NULL)
1285 return NULL;
1286
1287 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001288 if (mod != NULL)
1289 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001290 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001292}
1293
1294PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001295PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001296 PyObject *locals, int closeit, PyCompilerFlags *flags)
1297{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001299 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001300 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001301 if (arena == NULL)
1302 return NULL;
1303
1304 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1305 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001306 if (closeit)
1307 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001308 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001309 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001311 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001312 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001313 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001315}
1316
Guido van Rossum82598051997-03-05 00:20:32 +00001317static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001319 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001320{
Guido van Rossum82598051997-03-05 00:20:32 +00001321 PyCodeObject *co;
1322 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001324 if (co == NULL)
1325 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001326 v = PyEval_EvalCode(co, globals, locals);
1327 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001328 return v;
1329}
1330
Guido van Rossum82598051997-03-05 00:20:32 +00001331static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001332run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001334{
Guido van Rossum82598051997-03-05 00:20:32 +00001335 PyCodeObject *co;
1336 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001337 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001338 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001339
Guido van Rossum82598051997-03-05 00:20:32 +00001340 magic = PyMarshal_ReadLongFromFile(fp);
1341 if (magic != PyImport_GetMagicNumber()) {
1342 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001343 "Bad magic number in .pyc file");
1344 return NULL;
1345 }
Guido van Rossum82598051997-03-05 00:20:32 +00001346 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001347 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001348 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001349 if (v == NULL || !PyCode_Check(v)) {
1350 Py_XDECREF(v);
1351 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001352 "Bad code object in .pyc file");
1353 return NULL;
1354 }
Guido van Rossum82598051997-03-05 00:20:32 +00001355 co = (PyCodeObject *)v;
1356 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001357 if (v && flags)
1358 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001359 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001360 return v;
1361}
1362
Guido van Rossum82598051997-03-05 00:20:32 +00001363PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001364Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001365 PyCompilerFlags *flags)
1366{
Guido van Rossum82598051997-03-05 00:20:32 +00001367 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001368 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001369 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001370 if (arena == NULL)
1371 return NULL;
1372
1373 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001374 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001375 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001376 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001377 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001378 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001379 PyObject *result = PyAST_mod2obj(mod);
1380 PyArena_Free(arena);
1381 return result;
1382 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001383 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001384 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001385 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001386}
1387
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001388struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001389Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001390{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001391 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001392 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001393 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001394 if (arena == NULL)
1395 return NULL;
1396
1397 mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001398 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001399 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001400 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001403 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001404 return st;
1405}
1406
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407/* Preferred access to parser is through AST. */
1408mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001409PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 mod_ty mod;
1413 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 node *n = PyParser_ParseStringFlagsFilename(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001415 &_PyParser_Grammar, start, &err,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001416 PARSER_FLAGS(flags));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 if (n) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001418 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 PyNode_Free(n);
1420 return mod;
1421 }
1422 else {
1423 err_input(&err);
1424 return NULL;
1425 }
1426}
1427
1428mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001429PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001430 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001431 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 mod_ty mod;
1434 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001435 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1436 start, ps1, ps2, &err, PARSER_FLAGS(flags));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 if (n) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001438 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 PyNode_Free(n);
1440 return mod;
1441 }
1442 else {
1443 err_input(&err);
1444 if (errcode)
1445 *errcode = err.error;
1446 return NULL;
1447 }
1448}
1449
Guido van Rossuma110aa61994-08-29 12:50:44 +00001450/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001451
Guido van Rossuma110aa61994-08-29 12:50:44 +00001452node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001453PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001454{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001455 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001456 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1457 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001458 if (n == NULL)
1459 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001460
Guido van Rossuma110aa61994-08-29 12:50:44 +00001461 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001462}
1463
Guido van Rossuma110aa61994-08-29 12:50:44 +00001464/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001465
Guido van Rossuma110aa61994-08-29 12:50:44 +00001466node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001467PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001468{
Tim Petersfe2127d2001-07-16 05:37:24 +00001469 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001470 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1471 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001472 if (n == NULL)
1473 err_input(&err);
1474 return n;
1475}
1476
1477node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001478PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001479 int start, int flags)
1480{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001481 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001482 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1483 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001484 if (n == NULL)
1485 err_input(&err);
1486 return n;
1487}
1488
1489node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001490PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001491{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001492 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001493}
1494
Guido van Rossum66ebd912003-04-17 16:02:26 +00001495/* May want to move a more generalized form of this to parsetok.c or
1496 even parser modules. */
1497
1498void
1499PyParser_SetError(perrdetail *err)
1500{
1501 err_input(err);
1502}
1503
Guido van Rossuma110aa61994-08-29 12:50:44 +00001504/* Set the error appropriate to the given input error code (see errcode.h) */
1505
1506static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001507err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001508{
Fred Drake85f36392000-07-11 17:53:00 +00001509 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001510 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001511 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001512 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001513 switch (err->error) {
1514 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001515 errtype = PyExc_IndentationError;
1516 if (err->expected == INDENT)
1517 msg = "expected an indented block";
1518 else if (err->token == INDENT)
1519 msg = "unexpected indent";
1520 else if (err->token == DEDENT)
1521 msg = "unexpected unindent";
1522 else {
1523 errtype = PyExc_SyntaxError;
1524 msg = "invalid syntax";
1525 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001526 break;
1527 case E_TOKEN:
1528 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001529 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001530 case E_EOFS:
1531 msg = "EOF while scanning triple-quoted string";
1532 break;
1533 case E_EOLS:
1534 msg = "EOL while scanning single-quoted string";
1535 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001536 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001537 if (!PyErr_Occurred())
1538 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001539 return;
1540 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001541 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001542 return;
1543 case E_EOF:
1544 msg = "unexpected EOF while parsing";
1545 break;
Fred Drake85f36392000-07-11 17:53:00 +00001546 case E_TABSPACE:
1547 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001548 msg = "inconsistent use of tabs and spaces in indentation";
1549 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001550 case E_OVERFLOW:
1551 msg = "expression too long";
1552 break;
Fred Drake85f36392000-07-11 17:53:00 +00001553 case E_DEDENT:
1554 errtype = PyExc_IndentationError;
1555 msg = "unindent does not match any outer indentation level";
1556 break;
1557 case E_TOODEEP:
1558 errtype = PyExc_IndentationError;
1559 msg = "too many levels of indentation";
1560 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001561 case E_DECODE: {
1562 PyObject *type, *value, *tb;
1563 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001564 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001565 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001566 if (u != NULL) {
1567 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001568 }
1569 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001570 if (msg == NULL)
1571 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001572 Py_XDECREF(type);
1573 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001574 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001575 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001576 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001577 case E_LINECONT:
1578 msg = "unexpected character after line continuation character";
1579 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001580 default:
1581 fprintf(stderr, "error=%d\n", err->error);
1582 msg = "unknown parsing error";
1583 break;
1584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 v = Py_BuildValue("(ziiz)", err->filename,
1586 err->lineno, err->offset, err->text);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001587 if (err->text != NULL) {
Tim Petersc9d78aa2006-03-26 23:27:58 +00001588 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 err->text = NULL;
1590 }
1591 w = NULL;
1592 if (v != NULL)
1593 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001594 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001595 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001596 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001597 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001598}
1599
1600/* Print fatal error message and abort */
1601
1602void
Tim Peters7c321a82002-07-09 02:57:01 +00001603Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001604{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001605 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001606#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001607 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001608 OutputDebugString(msg);
1609 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001610#ifdef _DEBUG
1611 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001612#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001613#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001614 abort();
1615}
1616
1617/* Clean up and exit */
1618
Guido van Rossuma110aa61994-08-29 12:50:44 +00001619#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001620#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001621#endif
1622
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001623#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001624static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001625static int nexitfuncs = 0;
1626
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001627int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001628{
1629 if (nexitfuncs >= NEXITFUNCS)
1630 return -1;
1631 exitfuncs[nexitfuncs++] = func;
1632 return 0;
1633}
1634
Guido van Rossumcc283f51997-08-05 02:22:03 +00001635static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001636call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001637{
Guido van Rossum82598051997-03-05 00:20:32 +00001638 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001639
1640 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001641 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001642 Py_INCREF(exitfunc);
1643 PySys_SetObject("exitfunc", (PyObject *)NULL);
1644 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001645 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001646 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1647 PySys_WriteStderr("Error in sys.exitfunc:\n");
1648 }
Guido van Rossum82598051997-03-05 00:20:32 +00001649 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001650 }
Guido van Rossum82598051997-03-05 00:20:32 +00001651 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001652 }
1653
Guido van Rossum0829c751998-02-28 04:31:39 +00001654 if (Py_FlushLine())
1655 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001656}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001657
Guido van Rossumcc283f51997-08-05 02:22:03 +00001658static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001659call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001660{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001661 while (nexitfuncs > 0)
1662 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001663
1664 fflush(stdout);
1665 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001666}
1667
1668void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001669Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001670{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001671 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001672
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001673 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001674}
1675
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001676static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001677initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001678{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001679#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001680 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001681#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001682#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001683 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001684#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001685#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001686 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001687#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001688 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001689}
1690
Guido van Rossum7433b121997-02-14 19:45:36 +00001691
1692/*
1693 * The file descriptor fd is considered ``interactive'' if either
1694 * a) isatty(fd) is TRUE, or
1695 * b) the -i flag was given, and the filename associated with
1696 * the descriptor is NULL or "<stdin>" or "???".
1697 */
1698int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001699Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001700{
1701 if (isatty((int)fileno(fp)))
1702 return 1;
1703 if (!Py_InteractiveFlag)
1704 return 0;
1705 return (filename == NULL) ||
1706 (strcmp(filename, "<stdin>") == 0) ||
1707 (strcmp(filename, "???") == 0);
1708}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001709
1710
Tim Petersd08e3822003-04-17 15:24:21 +00001711#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001712#if defined(WIN32) && defined(_MSC_VER)
1713
1714/* Stack checking for Microsoft C */
1715
1716#include <malloc.h>
1717#include <excpt.h>
1718
Fred Drakee8de31c2000-08-31 05:38:39 +00001719/*
1720 * Return non-zero when we run out of memory on the stack; zero otherwise.
1721 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001722int
Fred Drake399739f2000-08-31 05:52:44 +00001723PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001724{
1725 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001726 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001727 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001728 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001729 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001730 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1731 EXCEPTION_EXECUTE_HANDLER :
1732 EXCEPTION_CONTINUE_SEARCH) {
1733 int errcode = _resetstkoflw();
1734 if (errcode)
1735 {
1736 Py_FatalError("Could not reset the stack!");
1737 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001738 }
1739 return 1;
1740}
1741
1742#endif /* WIN32 && _MSC_VER */
1743
1744/* Alternate implementations can be added here... */
1745
1746#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001747
1748
1749/* Wrappers around sigaction() or signal(). */
1750
1751PyOS_sighandler_t
1752PyOS_getsig(int sig)
1753{
1754#ifdef HAVE_SIGACTION
1755 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001756 if (sigaction(sig, NULL, &context) == -1)
1757 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001758 return context.sa_handler;
1759#else
1760 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001761/* Special signal handling for the secure CRT in Visual Studio 2005 */
1762#if defined(_MSC_VER) && _MSC_VER >= 1400
1763 switch (sig) {
1764 /* Only these signals are valid */
1765 case SIGINT:
1766 case SIGILL:
1767 case SIGFPE:
1768 case SIGSEGV:
1769 case SIGTERM:
1770 case SIGBREAK:
1771 case SIGABRT:
1772 break;
1773 /* Don't call signal() with other values or it will assert */
1774 default:
1775 return SIG_ERR;
1776 }
1777#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001778 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001779 if (handler != SIG_ERR)
1780 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001781 return handler;
1782#endif
1783}
1784
1785PyOS_sighandler_t
1786PyOS_setsig(int sig, PyOS_sighandler_t handler)
1787{
1788#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001789 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001790 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001791 sigemptyset(&context.sa_mask);
1792 context.sa_flags = 0;
1793 if (sigaction(sig, &context, &ocontext) == -1)
1794 return SIG_ERR;
1795 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001796#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001797 PyOS_sighandler_t oldhandler;
1798 oldhandler = signal(sig, handler);
1799#ifdef HAVE_SIGINTERRUPT
1800 siginterrupt(sig, 1);
1801#endif
1802 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001803#endif
1804}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805
1806/* Deprecated C API functions still provided for binary compatiblity */
1807
1808#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001809PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1811{
1812 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1813}
1814
Thomas Heller1b046642006-04-18 18:51:06 +00001815#undef PyParser_SimpleParseString
1816PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817PyParser_SimpleParseString(const char *str, int start)
1818{
1819 return PyParser_SimpleParseStringFlags(str, start, 0);
1820}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001821
Thomas Heller1b046642006-04-18 18:51:06 +00001822#undef PyRun_AnyFile
1823PyAPI_FUNC(int)
1824PyRun_AnyFile(FILE *fp, const char *name)
1825{
1826 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1827}
1828
1829#undef PyRun_AnyFileEx
1830PyAPI_FUNC(int)
1831PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1832{
1833 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1834}
1835
1836#undef PyRun_AnyFileFlags
1837PyAPI_FUNC(int)
1838PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1839{
1840 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1841}
1842
1843#undef PyRun_File
1844PyAPI_FUNC(PyObject *)
1845PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1846{
1847 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1848}
1849
1850#undef PyRun_FileEx
1851PyAPI_FUNC(PyObject *)
1852PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1853{
1854 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1855}
1856
1857#undef PyRun_FileFlags
1858PyAPI_FUNC(PyObject *)
1859PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1860 PyCompilerFlags *flags)
1861{
1862 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1863}
1864
1865#undef PyRun_SimpleFile
1866PyAPI_FUNC(int)
1867PyRun_SimpleFile(FILE *f, const char *p)
1868{
1869 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1870}
1871
1872#undef PyRun_SimpleFileEx
1873PyAPI_FUNC(int)
1874PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1875{
1876 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1877}
1878
1879
1880#undef PyRun_String
1881PyAPI_FUNC(PyObject *)
1882PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1883{
1884 return PyRun_StringFlags(str, s, g, l, NULL);
1885}
1886
1887#undef PyRun_SimpleString
1888PyAPI_FUNC(int)
1889PyRun_SimpleString(const char *s)
1890{
1891 return PyRun_SimpleStringFlags(s, NULL);
1892}
1893
1894#undef Py_CompileString
1895PyAPI_FUNC(PyObject *)
1896Py_CompileString(const char *str, const char *p, int s)
1897{
1898 return Py_CompileStringFlags(str, p, s, NULL);
1899}
1900
1901#undef PyRun_InteractiveOne
1902PyAPI_FUNC(int)
1903PyRun_InteractiveOne(FILE *f, const char *p)
1904{
1905 return PyRun_InteractiveOneFlags(f, p, NULL);
1906}
1907
1908#undef PyRun_InteractiveLoop
1909PyAPI_FUNC(int)
1910PyRun_InteractiveLoop(FILE *f, const char *p)
1911{
1912 return PyRun_InteractiveLoopFlags(f, p, NULL);
1913}
1914
Anthony Baxterac6bd462006-04-13 02:06:09 +00001915#ifdef __cplusplus
1916}
1917#endif
1918