blob: 977ee214d6b2d81fb328e7dcb81b42ce86abfcfd [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
Georg Brandl734373c2009-01-03 21:55:17 +000025#ifdef MS_WINDOWS
26#include "malloc.h" /* for alloca */
27#endif
28
Martin v. Löwis73d538b2003-03-05 15:13:47 +000029#ifdef HAVE_LANGINFO_H
30#include <locale.h>
31#include <langinfo.h>
32#endif
33
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000034#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000035#undef BYTE
36#include "windows.h"
37#endif
38
Neal Norwitz4281cef2006-03-04 19:58:13 +000039#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000040#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000041#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000042#define PRINT_TOTAL_REFS() fprintf(stderr, \
43 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000044 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000045#endif
46
Anthony Baxterac6bd462006-04-13 02:06:09 +000047#ifdef __cplusplus
48extern "C" {
49#endif
50
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000051extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000052
Guido van Rossum82598051997-03-05 00:20:32 +000053extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000054
Guido van Rossumb73cc041993-11-01 16:28:59 +000055/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000056static void initmain(void);
57static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000059 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000060static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000061 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void err_input(perrdetail *);
63static void initsigs(void);
64static void call_sys_exitfunc(void);
65static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000066extern void _PyUnicode_Init(void);
67extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000068
Mark Hammond8d98d2c2003-04-19 15:41:53 +000069#ifdef WITH_THREAD
70extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
71extern void _PyGILState_Fini(void);
72#endif /* WITH_THREAD */
73
Guido van Rossum82598051997-03-05 00:20:32 +000074int Py_DebugFlag; /* Needed by parser.c */
75int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000076int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000077int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000078int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000079int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000080int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000081int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000082int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000083int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000084int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000085/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
86 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
87 true divisions (which they will be in 2.3). */
88int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000089int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000090
Brett Cannone9746892008-04-12 23:44:07 +000091/* PyModule_GetWarningsModule is no longer necessary as of 2.6
92since _warnings is builtin. This API should not be used. */
93PyObject *
94PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000095{
Brett Cannone9746892008-04-12 23:44:07 +000096 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000097}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000098
Guido van Rossum25ce5661997-08-02 03:10:38 +000099static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000100
Thomas Wouters7e474022000-07-16 12:04:32 +0000101/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000102
103int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000104Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000105{
106 return initialized;
107}
108
Guido van Rossum25ce5661997-08-02 03:10:38 +0000109/* Global initializations. Can be undone by Py_Finalize(). Don't
110 call this twice without an intervening Py_Finalize() call. When
111 initializations fail, a fatal error is issued and the function does
112 not return. On return, the first thread and interpreter state have
113 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115 Locking: you must hold the interpreter lock while calling this.
116 (If the lock has not yet been initialized, that's equivalent to
117 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000118
Guido van Rossum25ce5661997-08-02 03:10:38 +0000119*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000120
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000121static int
122add_flag(int flag, const char *envs)
123{
124 int env = atoi(envs);
125 if (flag < env)
126 flag = env;
127 if (flag < 1)
128 flag = 1;
129 return flag;
130}
131
Guido van Rossuma027efa1997-05-05 20:56:21 +0000132void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000133Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000135 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000136 PyThreadState *tstate;
137 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000138 char *p;
Neal Norwitz18aa3882008-08-24 05:04:52 +0000139 char *icodeset = NULL; /* On Windows, input codeset may theoretically
140 differ from output codeset. */
Martin v. Löwis99815892008-06-01 07:20:46 +0000141 char *codeset = NULL;
142 char *errors = NULL;
143 int free_codeset = 0;
144 int overridden = 0;
Martin v. Löwisb12d8572008-06-01 08:06:17 +0000145 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000146#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis99815892008-06-01 07:20:46 +0000147 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000148#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000149#ifdef MS_WINDOWS
150 char ibuf[128];
151 char buf[128];
152#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000153 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000154
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000155 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000156 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000157 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000158
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000159 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000160 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000161 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000162 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000163 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000164 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000165 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
166 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000167
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 interp = PyInterpreterState_New();
169 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000170 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000171
Guido van Rossuma027efa1997-05-05 20:56:21 +0000172 tstate = PyThreadState_New(interp);
173 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000174 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000175 (void) PyThreadState_Swap(tstate);
176
Guido van Rossum70d893a2001-08-16 08:21:42 +0000177 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000178
Neal Norwitzb2501f42002-12-31 03:42:13 +0000179 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000180 Py_FatalError("Py_Initialize: can't init frames");
181
Neal Norwitzb2501f42002-12-31 03:42:13 +0000182 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000183 Py_FatalError("Py_Initialize: can't init ints");
184
Christian Heimes3497f942008-05-26 12:29:14 +0000185 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000186 Py_FatalError("Py_Initialize: can't init bytearray");
187
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000188 _PyFloat_Init();
189
Guido van Rossum25ce5661997-08-02 03:10:38 +0000190 interp->modules = PyDict_New();
191 if (interp->modules == NULL)
192 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000193 interp->modules_reloading = PyDict_New();
194 if (interp->modules_reloading == NULL)
195 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000196
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000197#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000198 /* Init Unicode implementation; relies on the codec registry */
199 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000200#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000201
Barry Warsawf242aa02000-05-25 23:09:49 +0000202 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000203 if (bimod == NULL)
204 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000205 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000206 if (interp->builtins == NULL)
207 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000208 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000209
210 sysmod = _PySys_Init();
211 if (sysmod == NULL)
212 Py_FatalError("Py_Initialize: can't initialize sys");
213 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000214 if (interp->sysdict == NULL)
215 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000216 Py_INCREF(interp->sysdict);
217 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000218 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219 PyDict_SetItemString(interp->sysdict, "modules",
220 interp->modules);
221
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000222 _PyImport_Init();
223
Barry Warsawf242aa02000-05-25 23:09:49 +0000224 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000225 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000226 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000227
Barry Warsaw035574d1997-08-29 22:07:17 +0000228 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000229 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000230
Just van Rossum52e14d62002-12-30 22:08:05 +0000231 _PyImportHooks_Init();
232
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000233 if (install_sigs)
234 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000235
Georg Brandl3c0fd562008-07-05 10:07:18 +0000236 /* Initialize warnings. */
237 _PyWarnings_Init();
238 if (PySys_HasWarnOptions()) {
239 PyObject *warnings_module = PyImport_ImportModule("warnings");
240 if (!warnings_module)
241 PyErr_Clear();
242 Py_XDECREF(warnings_module);
243 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000244
245 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000246 if (!Py_NoSiteFlag)
247 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000248
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249 /* auto-thread-state API, if available */
250#ifdef WITH_THREAD
251 _PyGILState_Init(interp, tstate);
252#endif /* WITH_THREAD */
253
Martin v. Löwis99815892008-06-01 07:20:46 +0000254 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
255 p = icodeset = codeset = strdup(p);
256 free_codeset = 1;
257 errors = strchr(p, ':');
258 if (errors) {
259 *errors = '\0';
260 errors++;
261 }
262 overridden = 1;
263 }
264
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000265#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
266 /* On Unix, set the file system encoding according to the
267 user's preference, if the CODESET names a well-known
268 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000269 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000270 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271
Martin v. Löwis99815892008-06-01 07:20:46 +0000272 if (!overridden || !Py_FileSystemDefaultEncoding) {
273 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
274 setlocale(LC_CTYPE, "");
275 loc_codeset = nl_langinfo(CODESET);
276 if (loc_codeset && *loc_codeset) {
277 PyObject *enc = PyCodec_Encoder(loc_codeset);
278 if (enc) {
279 loc_codeset = strdup(loc_codeset);
280 Py_DECREF(enc);
281 } else {
282 loc_codeset = NULL;
283 PyErr_Clear();
284 }
285 } else
286 loc_codeset = NULL;
287 setlocale(LC_CTYPE, saved_locale);
288 free(saved_locale);
289
290 if (!overridden) {
291 codeset = icodeset = loc_codeset;
292 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000293 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000294
295 /* Initialize Py_FileSystemDefaultEncoding from
296 locale even if PYTHONIOENCODING is set. */
297 if (!Py_FileSystemDefaultEncoding) {
298 Py_FileSystemDefaultEncoding = loc_codeset;
299 if (!overridden)
300 free_codeset = 0;
301 }
302 }
303#endif
304
305#ifdef MS_WINDOWS
306 if (!overridden) {
307 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000308 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000309 sprintf(ibuf, "cp%d", GetConsoleCP());
310 sprintf(buf, "cp%d", GetConsoleOutputCP());
311 }
312#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000313
314 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000315 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000316 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
317 if (!sys_isatty)
318 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000319 if ((overridden ||
320 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000321 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000322 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000323 Py_FatalError("Cannot set codeset of stdin");
324 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000325 Py_XDECREF(sys_isatty);
326
327 sys_stream = PySys_GetObject("stdout");
328 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
329 if (!sys_isatty)
330 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000331 if ((overridden ||
332 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000333 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000334 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000335 Py_FatalError("Cannot set codeset of stdout");
336 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000337 Py_XDECREF(sys_isatty);
338
Martin v. Löwisea62d252006-04-03 10:56:49 +0000339 sys_stream = PySys_GetObject("stderr");
340 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
341 if (!sys_isatty)
342 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000343 if((overridden ||
344 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000345 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000346 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000347 Py_FatalError("Cannot set codeset of stderr");
348 }
349 Py_XDECREF(sys_isatty);
350
Martin v. Löwis99815892008-06-01 07:20:46 +0000351 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000352 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000353 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000354}
355
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000356void
357Py_Initialize(void)
358{
359 Py_InitializeEx(1);
360}
361
362
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000363#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000364extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000365#endif
366
Guido van Rossum25ce5661997-08-02 03:10:38 +0000367/* Undo the effect of Py_Initialize().
368
369 Beware: if multiple interpreter and/or thread states exist, these
370 are not wiped out; only the current thread and interpreter state
371 are deleted. But since everything else is deleted, those other
372 interpreter and thread states should no longer be used.
373
374 (XXX We should do better, e.g. wipe out all interpreters and
375 threads.)
376
377 Locking: as above.
378
379*/
380
381void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000383{
384 PyInterpreterState *interp;
385 PyThreadState *tstate;
386
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000387 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000388 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000389
Tim Peters384fd102001-01-21 03:40:37 +0000390 /* The interpreter is still entirely intact at this point, and the
391 * exit funcs may be relying on that. In particular, if some thread
392 * or exit func is still waiting to do an import, the import machinery
393 * expects Py_IsInitialized() to return true. So don't say the
394 * interpreter is uninitialized until after the exit funcs have run.
395 * Note that Threading.py uses an exit func to do a join on all the
396 * threads created thru it, so this also protects pending imports in
397 * the threads created via Threading.
398 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000399 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000400 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000401
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000402 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000403 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404 interp = tstate->interp;
405
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000406 /* Disable signal handling */
407 PyOS_FiniInterrupts();
408
Christian Heimes908caac2008-01-27 23:34:59 +0000409 /* Clear type lookup cache */
410 PyType_ClearCache();
411
Guido van Rossume13ddc92003-04-17 17:29:22 +0000412 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000413 * before all modules are destroyed.
414 * XXX If a __del__ or weakref callback is triggered here, and tries to
415 * XXX import a module, bad things can happen, because Python no
416 * XXX longer believes it's initialized.
417 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
418 * XXX is easy to provoke that way. I've also seen, e.g.,
419 * XXX Exception exceptions.ImportError: 'No module named sha'
420 * XXX in <function callback at 0x008F5718> ignored
421 * XXX but I'm unclear on exactly how that one happens. In any case,
422 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000423 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000424 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000425#ifdef COUNT_ALLOCS
426 /* With COUNT_ALLOCS, it helps to run GC multiple times:
427 each collection might release some types from the type
428 list, so they become garbage. */
429 while (PyGC_Collect() > 0)
430 /* nothing */;
431#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000432
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000433 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000434 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000435
Guido van Rossume13ddc92003-04-17 17:29:22 +0000436 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000437 * new-style class definitions, for example.
438 * XXX This is disabled because it caused too many problems. If
439 * XXX a __del__ or weakref callback triggers here, Python code has
440 * XXX a hard time running, because even the sys module has been
441 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
442 * XXX One symptom is a sequence of information-free messages
443 * XXX coming from threads (if a __del__ or callback is invoked,
444 * XXX other threads can execute too, and any exception they encounter
445 * XXX triggers a comedy of errors as subsystem after subsystem
446 * XXX fails to find what it *expects* to find in sys to help report
447 * XXX the exception and consequent unexpected failures). I've also
448 * XXX seen segfaults then, after adding print statements to the
449 * XXX Python code getting called.
450 */
451#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000452 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000453#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000454
Guido van Rossum1707aad1997-12-08 23:43:45 +0000455 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
456 _PyImport_Fini();
457
458 /* Debugging stuff */
459#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000460 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000461#endif
462
Tim Peters62e97f02006-03-28 21:44:32 +0000463 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000464
Tim Peters9cf25ce2003-04-17 15:21:01 +0000465#ifdef Py_TRACE_REFS
466 /* Display all objects still alive -- this can invoke arbitrary
467 * __repr__ overrides, so requires a mostly-intact interpreter.
468 * Alas, a lot of stuff may still be alive now that will be cleaned
469 * up later.
470 */
Tim Peters269b2a62003-04-17 19:52:29 +0000471 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000472 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000473#endif /* Py_TRACE_REFS */
474
Guido van Rossumd922fa42003-04-15 14:10:09 +0000475 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000476 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000477
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000478 /* Now we decref the exception classes. After this point nothing
479 can raise an exception. That's okay, because each Fini() method
480 below has been checked to make sure no exceptions are ever
481 raised.
482 */
483
484 _PyExc_Fini();
485
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000486 /* Cleanup auto-thread-state */
487#ifdef WITH_THREAD
488 _PyGILState_Fini();
489#endif /* WITH_THREAD */
490
Guido van Rossumd922fa42003-04-15 14:10:09 +0000491 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000492 PyThreadState_Swap(NULL);
493 PyInterpreterState_Delete(interp);
494
Guido van Rossumd922fa42003-04-15 14:10:09 +0000495 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000496 PyMethod_Fini();
497 PyFrame_Fini();
498 PyCFunction_Fini();
499 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000500 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000501 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000502 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000503 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000504 PyInt_Fini();
505 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000506 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000507
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000508#ifdef Py_USING_UNICODE
509 /* Cleanup Unicode implementation */
510 _PyUnicode_Fini();
511#endif
512
Guido van Rossumcc283f51997-08-05 02:22:03 +0000513 /* XXX Still allocated:
514 - various static ad-hoc pointers to interned strings
515 - int and float free list blocks
516 - whatever various modules and libraries allocate
517 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000518
519 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000520
Tim Peters269b2a62003-04-17 19:52:29 +0000521#ifdef Py_TRACE_REFS
522 /* Display addresses (& refcnts) of all objects still alive.
523 * An address can be used to find the repr of the object, printed
524 * above by _Py_PrintReferences.
525 */
526 if (Py_GETENV("PYTHONDUMPREFS"))
527 _Py_PrintReferenceAddresses(stderr);
528#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000529#ifdef PYMALLOC_DEBUG
530 if (Py_GETENV("PYTHONMALLOCSTATS"))
531 _PyObject_DebugMallocStats();
532#endif
533
Guido van Rossumcc283f51997-08-05 02:22:03 +0000534 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000535}
536
537/* Create and initialize a new interpreter and thread, and return the
538 new thread. This requires that Py_Initialize() has been called
539 first.
540
541 Unsuccessful initialization yields a NULL pointer. Note that *no*
542 exception information is available even in this case -- the
543 exception information is held in the thread, and there is no
544 thread.
545
546 Locking: as above.
547
548*/
549
550PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000551Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000552{
553 PyInterpreterState *interp;
554 PyThreadState *tstate, *save_tstate;
555 PyObject *bimod, *sysmod;
556
557 if (!initialized)
558 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
559
560 interp = PyInterpreterState_New();
561 if (interp == NULL)
562 return NULL;
563
564 tstate = PyThreadState_New(interp);
565 if (tstate == NULL) {
566 PyInterpreterState_Delete(interp);
567 return NULL;
568 }
569
570 save_tstate = PyThreadState_Swap(tstate);
571
572 /* XXX The following is lax in error checking */
573
574 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000575 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576
577 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
578 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000579 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000580 if (interp->builtins == NULL)
581 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000582 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000583 }
584 sysmod = _PyImport_FindExtension("sys", "sys");
585 if (bimod != NULL && sysmod != NULL) {
586 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000587 if (interp->sysdict == NULL)
588 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589 Py_INCREF(interp->sysdict);
590 PySys_SetPath(Py_GetPath());
591 PyDict_SetItemString(interp->sysdict, "modules",
592 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000593 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000594 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000595 if (!Py_NoSiteFlag)
596 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000597 }
598
599 if (!PyErr_Occurred())
600 return tstate;
601
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000602handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000603 /* Oops, it didn't work. Undo it all. */
604
605 PyErr_Print();
606 PyThreadState_Clear(tstate);
607 PyThreadState_Swap(save_tstate);
608 PyThreadState_Delete(tstate);
609 PyInterpreterState_Delete(interp);
610
611 return NULL;
612}
613
614/* Delete an interpreter and its last thread. This requires that the
615 given thread state is current, that the thread has no remaining
616 frames, and that it is its interpreter's only remaining thread.
617 It is a fatal error to violate these constraints.
618
619 (Py_Finalize() doesn't have these constraints -- it zaps
620 everything, regardless.)
621
622 Locking: as above.
623
624*/
625
626void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000627Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000628{
629 PyInterpreterState *interp = tstate->interp;
630
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000631 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632 Py_FatalError("Py_EndInterpreter: thread is not current");
633 if (tstate->frame != NULL)
634 Py_FatalError("Py_EndInterpreter: thread still has a frame");
635 if (tstate != interp->tstate_head || tstate->next != NULL)
636 Py_FatalError("Py_EndInterpreter: not the last thread");
637
638 PyImport_Cleanup();
639 PyInterpreterState_Clear(interp);
640 PyThreadState_Swap(NULL);
641 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000642}
643
644static char *progname = "python";
645
646void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000647Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000648{
649 if (pn && *pn)
650 progname = pn;
651}
652
653char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000655{
656 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000657}
658
Guido van Rossuma61691e1998-02-06 22:27:24 +0000659static char *default_home = NULL;
660
661void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000662Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000663{
664 default_home = home;
665}
666
667char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000668Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000669{
670 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000671 if (home == NULL && !Py_IgnoreEnvironmentFlag)
672 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000673 return home;
674}
675
Guido van Rossum6135a871995-01-09 17:53:26 +0000676/* Create __main__ module */
677
678static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000679initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000680{
Guido van Rossum82598051997-03-05 00:20:32 +0000681 PyObject *m, *d;
682 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000683 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000684 Py_FatalError("can't create __main__ module");
685 d = PyModule_GetDict(m);
686 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000687 PyObject *bimod = PyImport_ImportModule("__builtin__");
688 if (bimod == NULL ||
689 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000690 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000691 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000692 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000693}
694
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000695/* Import the site module (not into __main__ though) */
696
697static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000698initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000699{
700 PyObject *m, *f;
701 m = PyImport_ImportModule("site");
702 if (m == NULL) {
703 f = PySys_GetObject("stderr");
704 if (Py_VerboseFlag) {
705 PyFile_WriteString(
706 "'import site' failed; traceback:\n", f);
707 PyErr_Print();
708 }
709 else {
710 PyFile_WriteString(
711 "'import site' failed; use -v for traceback\n", f);
712 PyErr_Clear();
713 }
714 }
715 else {
716 Py_DECREF(m);
717 }
718}
719
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000720/* Parse input from a file and execute it */
721
722int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000723PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000724 PyCompilerFlags *flags)
725{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000726 if (filename == NULL)
727 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000728 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000729 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000730 if (closeit)
731 fclose(fp);
732 return err;
733 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000734 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000735 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000736}
737
738int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000739PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000740{
Guido van Rossum82598051997-03-05 00:20:32 +0000741 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000742 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000743 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000744
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000745 if (flags == NULL) {
746 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000747 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000748 }
Guido van Rossum82598051997-03-05 00:20:32 +0000749 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000750 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000751 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000752 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753 }
Guido van Rossum82598051997-03-05 00:20:32 +0000754 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000755 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000756 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000757 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758 }
759 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000760 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000761 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000762 if (ret == E_EOF)
763 return 0;
764 /*
765 if (ret == E_NOMEM)
766 return -1;
767 */
768 }
769}
770
Eric Smith7c478942008-03-18 23:45:49 +0000771#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000772/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000773#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000774 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000775 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000776#endif
777#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000778/* Keep an example of flags with future keyword support. */
779#define PARSER_FLAGS(flags) \
780 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000781 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000782 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
783 PyPARSE_PRINT_IS_FUNCTION : 0) \
784 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
785 PyPARSE_UNICODE_LITERALS : 0) \
786 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000787#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000788
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000789int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000790PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000791{
Guido van Rossum82598051997-03-05 00:20:32 +0000792 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000794 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000795 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000797
Guido van Rossum82598051997-03-05 00:20:32 +0000798 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000799 if (v != NULL) {
800 v = PyObject_Str(v);
801 if (v == NULL)
802 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000803 else if (PyString_Check(v))
804 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000805 }
Guido van Rossum82598051997-03-05 00:20:32 +0000806 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000807 if (w != NULL) {
808 w = PyObject_Str(w);
809 if (w == NULL)
810 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000811 else if (PyString_Check(w))
812 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000813 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000814 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000815 if (arena == NULL) {
816 Py_XDECREF(v);
817 Py_XDECREF(w);
818 return -1;
819 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000820 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000822 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000823 Py_XDECREF(v);
824 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000826 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 if (errcode == E_EOF) {
828 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000829 return E_EOF;
830 }
Guido van Rossum82598051997-03-05 00:20:32 +0000831 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000836 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000837 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000838 }
Guido van Rossum82598051997-03-05 00:20:32 +0000839 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000840 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000841 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000842 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000843 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000844 return -1;
845 }
Guido van Rossum82598051997-03-05 00:20:32 +0000846 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000847 if (Py_FlushLine())
848 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000849 return 0;
850}
851
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000852/* Check whether a file maybe a pyc file: Look at the extension,
853 the file type, and, if we may close it, at the first few bytes. */
854
855static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000856maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000857{
858 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
859 return 1;
860
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000861 /* Only look into the file if we are allowed to close it, since
862 it then should also be seekable. */
863 if (closeit) {
864 /* Read only two bytes of the magic. If the file was opened in
865 text mode, the bytes 3 and 4 of the magic (\r\n) might not
866 be read as they are on disk. */
867 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
868 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000869 /* Mess: In case of -x, the stream is NOT at its start now,
870 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000871 which makes the current stream position formally undefined,
872 and a x-platform nightmare.
873 Unfortunately, we have no direct way to know whether -x
874 was specified. So we use a terrible hack: if the current
875 stream position is not 0, we assume -x was specified, and
876 give up. Bug 132850 on SourceForge spells out the
877 hopelessness of trying anything else (fseek and ftell
878 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000879 */
Tim Peters3e876562001-02-11 04:35:39 +0000880 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000881 if (ftell(fp) == 0) {
882 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000883 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000884 ispyc = 1;
885 rewind(fp);
886 }
Tim Peters3e876562001-02-11 04:35:39 +0000887 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000888 }
889 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000890}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000891
Guido van Rossum0df002c2000-08-27 19:21:52 +0000892int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000893PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000894 PyCompilerFlags *flags)
895{
Guido van Rossum82598051997-03-05 00:20:32 +0000896 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000897 const char *ext;
Matthias Klosea8da9e02009-04-04 14:19:56 +0000898 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000899
Guido van Rossum82598051997-03-05 00:20:32 +0000900 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000901 if (m == NULL)
902 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000903 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000904 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000905 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000906 if (f == NULL)
907 return -1;
908 if (PyDict_SetItemString(d, "__file__", f) < 0) {
909 Py_DECREF(f);
910 return -1;
911 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000912 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000913 Py_DECREF(f);
914 }
Matthias Klosea8da9e02009-04-04 14:19:56 +0000915 len = strlen(filename);
916 ext = filename + len - (len > 4 ? 4 : 0);
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000917 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000918 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000919 if (closeit)
920 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000921 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000922 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000923 ret = -1;
924 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000925 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000926 /* Turn on optimization if a .pyo file is given */
927 if (strcmp(ext, ".pyo") == 0)
928 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000929 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000930 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000931 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000932 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000933 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000934 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000935 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000936 ret = -1;
937 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000938 }
Guido van Rossum82598051997-03-05 00:20:32 +0000939 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000940 if (Py_FlushLine())
941 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000942 ret = 0;
943 done:
944 if (set_file_name && PyDict_DelItemString(d, "__file__"))
945 PyErr_Clear();
946 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000947}
948
949int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000950PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000951{
Guido van Rossum82598051997-03-05 00:20:32 +0000952 PyObject *m, *d, *v;
953 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000954 if (m == NULL)
955 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000956 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000957 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000958 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000959 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000960 return -1;
961 }
Guido van Rossum82598051997-03-05 00:20:32 +0000962 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000963 if (Py_FlushLine())
964 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000965 return 0;
966}
967
Barry Warsaw035574d1997-08-29 22:07:17 +0000968static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000969parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
970 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000971{
972 long hold;
973 PyObject *v;
974
975 /* old style errors */
976 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000977 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000978 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000979
980 /* new style errors. `err' is an instance */
981
982 if (! (v = PyObject_GetAttrString(err, "msg")))
983 goto finally;
984 *message = v;
985
986 if (!(v = PyObject_GetAttrString(err, "filename")))
987 goto finally;
988 if (v == Py_None)
989 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000990 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000991 goto finally;
992
993 Py_DECREF(v);
994 if (!(v = PyObject_GetAttrString(err, "lineno")))
995 goto finally;
996 hold = PyInt_AsLong(v);
997 Py_DECREF(v);
998 v = NULL;
999 if (hold < 0 && PyErr_Occurred())
1000 goto finally;
1001 *lineno = (int)hold;
1002
1003 if (!(v = PyObject_GetAttrString(err, "offset")))
1004 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001005 if (v == Py_None) {
1006 *offset = -1;
1007 Py_DECREF(v);
1008 v = NULL;
1009 } else {
1010 hold = PyInt_AsLong(v);
1011 Py_DECREF(v);
1012 v = NULL;
1013 if (hold < 0 && PyErr_Occurred())
1014 goto finally;
1015 *offset = (int)hold;
1016 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001017
1018 if (!(v = PyObject_GetAttrString(err, "text")))
1019 goto finally;
1020 if (v == Py_None)
1021 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001022 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001023 goto finally;
1024 Py_DECREF(v);
1025 return 1;
1026
1027finally:
1028 Py_XDECREF(v);
1029 return 0;
1030}
1031
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001032void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001033PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001034{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001035 PyErr_PrintEx(1);
1036}
1037
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001038static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001039print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001040{
1041 char *nl;
1042 if (offset >= 0) {
1043 if (offset > 0 && offset == (int)strlen(text))
1044 offset--;
1045 for (;;) {
1046 nl = strchr(text, '\n');
1047 if (nl == NULL || nl-text >= offset)
1048 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001049 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001050 text = nl+1;
1051 }
1052 while (*text == ' ' || *text == '\t') {
1053 text++;
1054 offset--;
1055 }
1056 }
1057 PyFile_WriteString(" ", f);
1058 PyFile_WriteString(text, f);
1059 if (*text == '\0' || text[strlen(text)-1] != '\n')
1060 PyFile_WriteString("\n", f);
1061 if (offset == -1)
1062 return;
1063 PyFile_WriteString(" ", f);
1064 offset--;
1065 while (offset > 0) {
1066 PyFile_WriteString(" ", f);
1067 offset--;
1068 }
1069 PyFile_WriteString("^\n", f);
1070}
1071
Guido van Rossum66e8e862001-03-23 17:54:43 +00001072static void
1073handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001074{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001075 PyObject *exception, *value, *tb;
1076 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001077
Georg Brandl49aafc92007-03-07 00:34:46 +00001078 if (Py_InspectFlag)
1079 /* Don't exit if -i flag was given. This flag is set to 0
1080 * when entering interactive mode for inspecting. */
1081 return;
1082
Guido van Rossum66e8e862001-03-23 17:54:43 +00001083 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001084 if (Py_FlushLine())
1085 PyErr_Clear();
1086 fflush(stdout);
1087 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001088 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001089 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001090 /* The error code should be in the `code' attribute. */
1091 PyObject *code = PyObject_GetAttrString(value, "code");
1092 if (code) {
1093 Py_DECREF(value);
1094 value = code;
1095 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001096 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001097 }
1098 /* If we failed to dig out the 'code' attribute,
1099 just let the else clause below print the error. */
1100 }
1101 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001102 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001103 else {
1104 PyObject_Print(value, stderr, Py_PRINT_RAW);
1105 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001106 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001107 }
Tim Peterscf615b52003-04-19 18:47:02 +00001108 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001109 /* Restore and clear the exception info, in order to properly decref
1110 * the exception, value, and traceback. If we just exit instead,
1111 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1112 * some finalizers from running.
1113 */
Tim Peterscf615b52003-04-19 18:47:02 +00001114 PyErr_Restore(exception, value, tb);
1115 PyErr_Clear();
1116 Py_Exit(exitcode);
1117 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001118}
1119
1120void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001121PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001122{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001123 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001124
1125 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1126 handle_system_exit();
1127 }
Guido van Rossum82598051997-03-05 00:20:32 +00001128 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 if (exception == NULL)
1130 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001131 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001132 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001133 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001134 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001135 if (set_sys_last_vars) {
1136 PySys_SetObject("last_type", exception);
1137 PySys_SetObject("last_value", v);
1138 PySys_SetObject("last_traceback", tb);
1139 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001140 hook = PySys_GetObject("excepthook");
1141 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001142 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001143 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001144 PyObject *result = PyEval_CallObject(hook, args);
1145 if (result == NULL) {
1146 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001147 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1148 handle_system_exit();
1149 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001150 PyErr_Fetch(&exception2, &v2, &tb2);
1151 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001152 /* It should not be possible for exception2 or v2
1153 to be NULL. However PyErr_Display() can't
1154 tolerate NULLs, so just be safe. */
1155 if (exception2 == NULL) {
1156 exception2 = Py_None;
1157 Py_INCREF(exception2);
1158 }
1159 if (v2 == NULL) {
1160 v2 = Py_None;
1161 Py_INCREF(v2);
1162 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001163 if (Py_FlushLine())
1164 PyErr_Clear();
1165 fflush(stdout);
1166 PySys_WriteStderr("Error in sys.excepthook:\n");
1167 PyErr_Display(exception2, v2, tb2);
1168 PySys_WriteStderr("\nOriginal exception was:\n");
1169 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001170 Py_DECREF(exception2);
1171 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001172 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001173 }
1174 Py_XDECREF(result);
1175 Py_XDECREF(args);
1176 } else {
1177 PySys_WriteStderr("sys.excepthook is missing\n");
1178 PyErr_Display(exception, v, tb);
1179 }
1180 Py_XDECREF(exception);
1181 Py_XDECREF(v);
1182 Py_XDECREF(tb);
1183}
1184
Richard Jones7b9558d2006-05-27 12:29:24 +00001185void
1186PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001187{
1188 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001189 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001190 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001191 if (f == NULL)
1192 fprintf(stderr, "lost sys.stderr\n");
1193 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001194 if (Py_FlushLine())
1195 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001196 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001197 if (tb && tb != Py_None)
1198 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001199 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001200 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001201 {
Guido van Rossum82598051997-03-05 00:20:32 +00001202 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001203 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001204 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001205 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001206 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001207 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001208 else {
1209 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001210 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001211 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001212 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001213 else
Guido van Rossum82598051997-03-05 00:20:32 +00001214 PyFile_WriteString(filename, f);
1215 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001216 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001217 PyFile_WriteString(buf, f);
1218 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001219 if (text != NULL)
1220 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001221 Py_DECREF(value);
1222 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001223 /* Can't be bothered to check all those
1224 PyFile_WriteString() calls */
1225 if (PyErr_Occurred())
1226 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001227 }
1228 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001229 if (err) {
1230 /* Don't do anything else */
1231 }
Brett Cannonbf364092006-03-01 04:25:17 +00001232 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001233 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001234 char* className = PyExceptionClass_Name(exception);
1235 if (className != NULL) {
1236 char *dot = strrchr(className, '.');
1237 if (dot != NULL)
1238 className = dot+1;
1239 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001240
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001241 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001242 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001243 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001244 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001245 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001246 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001247 {
1248 err = PyFile_WriteString(modstr, f);
1249 err += PyFile_WriteString(".", f);
1250 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001251 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001252 }
1253 if (err == 0) {
1254 if (className == NULL)
1255 err = PyFile_WriteString("<unknown>", f);
1256 else
Brett Cannonbf364092006-03-01 04:25:17 +00001257 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001258 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001259 }
1260 else
1261 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001262 if (err == 0 && (value != Py_None)) {
1263 PyObject *s = PyObject_Str(value);
1264 /* only print colon if the str() of the
1265 object is not the empty string
1266 */
1267 if (s == NULL)
1268 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001269 else if (!PyString_Check(s) ||
1270 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001271 err = PyFile_WriteString(": ", f);
1272 if (err == 0)
1273 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1274 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001275 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001276 /* try to write a newline in any case */
1277 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001278 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001279 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001280 /* If an error happened here, don't show it.
1281 XXX This is wrong, but too many callers rely on this behavior. */
1282 if (err != 0)
1283 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001284}
1285
Guido van Rossum82598051997-03-05 00:20:32 +00001286PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001287PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001289{
Neal Norwitze92fba02006-03-04 18:52:26 +00001290 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001291 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001292 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001293 if (arena == NULL)
1294 return NULL;
1295
1296 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001297 if (mod != NULL)
1298 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001299 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001301}
1302
1303PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001304PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001305 PyObject *locals, int closeit, PyCompilerFlags *flags)
1306{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001308 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001309 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001310 if (arena == NULL)
1311 return NULL;
1312
1313 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1314 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001315 if (closeit)
1316 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001317 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001318 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001320 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001321 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001322 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001324}
1325
Guido van Rossum82598051997-03-05 00:20:32 +00001326static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001328 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001329{
Guido van Rossum82598051997-03-05 00:20:32 +00001330 PyCodeObject *co;
1331 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001333 if (co == NULL)
1334 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001335 v = PyEval_EvalCode(co, globals, locals);
1336 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001337 return v;
1338}
1339
Guido van Rossum82598051997-03-05 00:20:32 +00001340static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001341run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001343{
Guido van Rossum82598051997-03-05 00:20:32 +00001344 PyCodeObject *co;
1345 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001346 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001347 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001348
Guido van Rossum82598051997-03-05 00:20:32 +00001349 magic = PyMarshal_ReadLongFromFile(fp);
1350 if (magic != PyImport_GetMagicNumber()) {
1351 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001352 "Bad magic number in .pyc file");
1353 return NULL;
1354 }
Guido van Rossum82598051997-03-05 00:20:32 +00001355 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001356 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001357 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001358 if (v == NULL || !PyCode_Check(v)) {
1359 Py_XDECREF(v);
1360 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001361 "Bad code object in .pyc file");
1362 return NULL;
1363 }
Guido van Rossum82598051997-03-05 00:20:32 +00001364 co = (PyCodeObject *)v;
1365 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001366 if (v && flags)
1367 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001368 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001369 return v;
1370}
1371
Guido van Rossum82598051997-03-05 00:20:32 +00001372PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001373Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001374 PyCompilerFlags *flags)
1375{
Guido van Rossum82598051997-03-05 00:20:32 +00001376 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001377 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001378 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001379 if (arena == NULL)
1380 return NULL;
1381
1382 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001383 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001384 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001385 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001386 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001387 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001388 PyObject *result = PyAST_mod2obj(mod);
1389 PyArena_Free(arena);
1390 return result;
1391 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001393 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001394 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001395}
1396
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001397struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001398Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001399{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001400 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001401 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001402 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001403 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001404 if (arena == NULL)
1405 return NULL;
1406
Christian Heimes7f23d862008-03-26 22:51:58 +00001407 flags.cf_flags = 0;
1408
Christian Heimes3c608332008-03-26 22:01:37 +00001409 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001411 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001412 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001413 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001415 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001416 return st;
1417}
1418
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419/* Preferred access to parser is through AST. */
1420mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001421PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001422 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001425 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001427 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001428
1429 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001430 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001431 &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001432 if (flags == NULL) {
1433 localflags.cf_flags = 0;
1434 flags = &localflags;
1435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001437 flags->cf_flags |= iflags & PyCF_MASK;
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 return NULL;
1445 }
1446}
1447
1448mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001449PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001450 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001451 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001454 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001456 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001457
Christian Heimes3c608332008-03-26 22:01:37 +00001458 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1459 start, ps1, ps2, &err, &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001460 if (flags == NULL) {
1461 localflags.cf_flags = 0;
1462 flags = &localflags;
1463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001465 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001466 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 PyNode_Free(n);
1468 return mod;
1469 }
1470 else {
1471 err_input(&err);
1472 if (errcode)
1473 *errcode = err.error;
1474 return NULL;
1475 }
1476}
1477
Guido van Rossuma110aa61994-08-29 12:50:44 +00001478/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001479
Guido van Rossuma110aa61994-08-29 12:50:44 +00001480node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001481PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001482{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001483 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001484 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1485 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001486 if (n == NULL)
1487 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001488
Guido van Rossuma110aa61994-08-29 12:50:44 +00001489 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001490}
1491
Guido van Rossuma110aa61994-08-29 12:50:44 +00001492/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001493
Guido van Rossuma110aa61994-08-29 12:50:44 +00001494node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001495PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001496{
Tim Petersfe2127d2001-07-16 05:37:24 +00001497 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001498 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1499 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001500 if (n == NULL)
1501 err_input(&err);
1502 return n;
1503}
1504
1505node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001506PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001507 int start, int flags)
1508{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001509 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001510 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1511 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001512 if (n == NULL)
1513 err_input(&err);
1514 return n;
1515}
1516
1517node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001518PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001519{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001520 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001521}
1522
Guido van Rossum66ebd912003-04-17 16:02:26 +00001523/* May want to move a more generalized form of this to parsetok.c or
1524 even parser modules. */
1525
1526void
1527PyParser_SetError(perrdetail *err)
1528{
1529 err_input(err);
1530}
1531
Guido van Rossuma110aa61994-08-29 12:50:44 +00001532/* Set the error appropriate to the given input error code (see errcode.h) */
1533
1534static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001535err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001536{
Fred Drake85f36392000-07-11 17:53:00 +00001537 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001538 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001539 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001540 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001541 switch (err->error) {
1542 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001543 errtype = PyExc_IndentationError;
1544 if (err->expected == INDENT)
1545 msg = "expected an indented block";
1546 else if (err->token == INDENT)
1547 msg = "unexpected indent";
1548 else if (err->token == DEDENT)
1549 msg = "unexpected unindent";
1550 else {
1551 errtype = PyExc_SyntaxError;
1552 msg = "invalid syntax";
1553 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001554 break;
1555 case E_TOKEN:
1556 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001557 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001558 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001559 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001560 break;
1561 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001562 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001563 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001564 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001565 if (!PyErr_Occurred())
1566 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001567 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001568 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001569 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001570 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001571 case E_EOF:
1572 msg = "unexpected EOF while parsing";
1573 break;
Fred Drake85f36392000-07-11 17:53:00 +00001574 case E_TABSPACE:
1575 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001576 msg = "inconsistent use of tabs and spaces in indentation";
1577 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001578 case E_OVERFLOW:
1579 msg = "expression too long";
1580 break;
Fred Drake85f36392000-07-11 17:53:00 +00001581 case E_DEDENT:
1582 errtype = PyExc_IndentationError;
1583 msg = "unindent does not match any outer indentation level";
1584 break;
1585 case E_TOODEEP:
1586 errtype = PyExc_IndentationError;
1587 msg = "too many levels of indentation";
1588 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001589 case E_DECODE: {
1590 PyObject *type, *value, *tb;
1591 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001592 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001593 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001594 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001595 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001596 }
1597 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001598 if (msg == NULL)
1599 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001600 Py_XDECREF(type);
1601 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001602 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001603 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001604 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001605 case E_LINECONT:
1606 msg = "unexpected character after line continuation character";
1607 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001608 default:
1609 fprintf(stderr, "error=%d\n", err->error);
1610 msg = "unknown parsing error";
1611 break;
1612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 v = Py_BuildValue("(ziiz)", err->filename,
1614 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 w = NULL;
1616 if (v != NULL)
1617 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001618 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001619 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001620 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001621 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001622cleanup:
1623 if (err->text != NULL) {
1624 PyObject_FREE(err->text);
1625 err->text = NULL;
1626 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001627}
1628
1629/* Print fatal error message and abort */
1630
1631void
Tim Peters7c321a82002-07-09 02:57:01 +00001632Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001633{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001634 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller70b11f02009-03-31 22:25:20 +00001635 fflush(stderr); /* it helps in Windows debug build */
1636
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001637#ifdef MS_WINDOWS
Georg Brandl734373c2009-01-03 21:55:17 +00001638 {
1639 size_t len = strlen(msg);
1640 WCHAR* buffer;
1641 size_t i;
1642
1643 /* Convert the message to wchar_t. This uses a simple one-to-one
1644 conversion, assuming that the this error message actually uses ASCII
1645 only. If this ceases to be true, we will have to convert. */
1646 buffer = alloca( (len+1) * (sizeof *buffer));
1647 for( i=0; i<=len; ++i)
1648 buffer[i] = msg[i];
1649 OutputDebugStringW(L"Fatal Python error: ");
1650 OutputDebugStringW(buffer);
1651 OutputDebugStringW(L"\n");
1652 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001653#ifdef _DEBUG
1654 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001655#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001656#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001657 abort();
1658}
1659
1660/* Clean up and exit */
1661
Guido van Rossuma110aa61994-08-29 12:50:44 +00001662#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001663#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001664#endif
1665
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001666#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001667static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001668static int nexitfuncs = 0;
1669
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001670int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001671{
1672 if (nexitfuncs >= NEXITFUNCS)
1673 return -1;
1674 exitfuncs[nexitfuncs++] = func;
1675 return 0;
1676}
1677
Guido van Rossumcc283f51997-08-05 02:22:03 +00001678static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001679call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001680{
Guido van Rossum82598051997-03-05 00:20:32 +00001681 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001682
1683 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001684 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001685 Py_INCREF(exitfunc);
1686 PySys_SetObject("exitfunc", (PyObject *)NULL);
1687 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001688 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001689 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1690 PySys_WriteStderr("Error in sys.exitfunc:\n");
1691 }
Guido van Rossum82598051997-03-05 00:20:32 +00001692 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001693 }
Guido van Rossum82598051997-03-05 00:20:32 +00001694 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001695 }
1696
Guido van Rossum0829c751998-02-28 04:31:39 +00001697 if (Py_FlushLine())
1698 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001699}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001700
Guido van Rossumcc283f51997-08-05 02:22:03 +00001701static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001702call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001703{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001704 while (nexitfuncs > 0)
1705 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001706
1707 fflush(stdout);
1708 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001709}
1710
1711void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001712Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001713{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001714 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001715
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001716 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001717}
1718
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001719static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001720initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001721{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001722#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001723 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001724#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001725#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001726 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001727#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001728#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001729 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001730#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001731 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001732}
1733
Guido van Rossum7433b121997-02-14 19:45:36 +00001734
1735/*
1736 * The file descriptor fd is considered ``interactive'' if either
1737 * a) isatty(fd) is TRUE, or
1738 * b) the -i flag was given, and the filename associated with
1739 * the descriptor is NULL or "<stdin>" or "???".
1740 */
1741int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001742Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001743{
1744 if (isatty((int)fileno(fp)))
1745 return 1;
1746 if (!Py_InteractiveFlag)
1747 return 0;
1748 return (filename == NULL) ||
1749 (strcmp(filename, "<stdin>") == 0) ||
1750 (strcmp(filename, "???") == 0);
1751}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001752
1753
Tim Petersd08e3822003-04-17 15:24:21 +00001754#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001755#if defined(WIN32) && defined(_MSC_VER)
1756
1757/* Stack checking for Microsoft C */
1758
1759#include <malloc.h>
1760#include <excpt.h>
1761
Fred Drakee8de31c2000-08-31 05:38:39 +00001762/*
1763 * Return non-zero when we run out of memory on the stack; zero otherwise.
1764 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001765int
Fred Drake399739f2000-08-31 05:52:44 +00001766PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001767{
1768 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001769 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001770 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001771 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001772 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001773 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1774 EXCEPTION_EXECUTE_HANDLER :
1775 EXCEPTION_CONTINUE_SEARCH) {
1776 int errcode = _resetstkoflw();
Amaury Forgeot d'Arc74b458c2008-11-22 20:06:51 +00001777 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001778 {
1779 Py_FatalError("Could not reset the stack!");
1780 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001781 }
1782 return 1;
1783}
1784
1785#endif /* WIN32 && _MSC_VER */
1786
1787/* Alternate implementations can be added here... */
1788
1789#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001790
1791
1792/* Wrappers around sigaction() or signal(). */
1793
1794PyOS_sighandler_t
1795PyOS_getsig(int sig)
1796{
1797#ifdef HAVE_SIGACTION
1798 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001799 if (sigaction(sig, NULL, &context) == -1)
1800 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001801 return context.sa_handler;
1802#else
1803 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001804/* Special signal handling for the secure CRT in Visual Studio 2005 */
1805#if defined(_MSC_VER) && _MSC_VER >= 1400
1806 switch (sig) {
1807 /* Only these signals are valid */
1808 case SIGINT:
1809 case SIGILL:
1810 case SIGFPE:
1811 case SIGSEGV:
1812 case SIGTERM:
1813 case SIGBREAK:
1814 case SIGABRT:
1815 break;
1816 /* Don't call signal() with other values or it will assert */
1817 default:
1818 return SIG_ERR;
1819 }
1820#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001821 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001822 if (handler != SIG_ERR)
1823 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001824 return handler;
1825#endif
1826}
1827
1828PyOS_sighandler_t
1829PyOS_setsig(int sig, PyOS_sighandler_t handler)
1830{
1831#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001832 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001833 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001834 sigemptyset(&context.sa_mask);
1835 context.sa_flags = 0;
1836 if (sigaction(sig, &context, &ocontext) == -1)
1837 return SIG_ERR;
1838 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001839#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001840 PyOS_sighandler_t oldhandler;
1841 oldhandler = signal(sig, handler);
1842#ifdef HAVE_SIGINTERRUPT
1843 siginterrupt(sig, 1);
1844#endif
1845 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001846#endif
1847}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848
1849/* Deprecated C API functions still provided for binary compatiblity */
1850
1851#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001852PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1854{
1855 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1856}
1857
Thomas Heller1b046642006-04-18 18:51:06 +00001858#undef PyParser_SimpleParseString
1859PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860PyParser_SimpleParseString(const char *str, int start)
1861{
1862 return PyParser_SimpleParseStringFlags(str, start, 0);
1863}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001864
Thomas Heller1b046642006-04-18 18:51:06 +00001865#undef PyRun_AnyFile
1866PyAPI_FUNC(int)
1867PyRun_AnyFile(FILE *fp, const char *name)
1868{
1869 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1870}
1871
1872#undef PyRun_AnyFileEx
1873PyAPI_FUNC(int)
1874PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1875{
1876 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1877}
1878
1879#undef PyRun_AnyFileFlags
1880PyAPI_FUNC(int)
1881PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1882{
1883 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1884}
1885
1886#undef PyRun_File
1887PyAPI_FUNC(PyObject *)
1888PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1889{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001890 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001891}
1892
1893#undef PyRun_FileEx
1894PyAPI_FUNC(PyObject *)
1895PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1896{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001897 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001898}
1899
1900#undef PyRun_FileFlags
1901PyAPI_FUNC(PyObject *)
1902PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1903 PyCompilerFlags *flags)
1904{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001905 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001906}
1907
1908#undef PyRun_SimpleFile
1909PyAPI_FUNC(int)
1910PyRun_SimpleFile(FILE *f, const char *p)
1911{
1912 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1913}
1914
1915#undef PyRun_SimpleFileEx
1916PyAPI_FUNC(int)
1917PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1918{
1919 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1920}
1921
1922
1923#undef PyRun_String
1924PyAPI_FUNC(PyObject *)
1925PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1926{
1927 return PyRun_StringFlags(str, s, g, l, NULL);
1928}
1929
1930#undef PyRun_SimpleString
1931PyAPI_FUNC(int)
1932PyRun_SimpleString(const char *s)
1933{
1934 return PyRun_SimpleStringFlags(s, NULL);
1935}
1936
1937#undef Py_CompileString
1938PyAPI_FUNC(PyObject *)
1939Py_CompileString(const char *str, const char *p, int s)
1940{
1941 return Py_CompileStringFlags(str, p, s, NULL);
1942}
1943
1944#undef PyRun_InteractiveOne
1945PyAPI_FUNC(int)
1946PyRun_InteractiveOne(FILE *f, const char *p)
1947{
1948 return PyRun_InteractiveOneFlags(f, p, NULL);
1949}
1950
1951#undef PyRun_InteractiveLoop
1952PyAPI_FUNC(int)
1953PyRun_InteractiveLoop(FILE *f, const char *p)
1954{
1955 return PyRun_InteractiveLoopFlags(f, p, NULL);
1956}
1957
Anthony Baxterac6bd462006-04-13 02:06:09 +00001958#ifdef __cplusplus
1959}
1960#endif
1961