blob: f2cd819b1c4d2de1e5300c88f43930ef48789b5d [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
Benjamin Peterson796798b2009-01-02 20:47:27 +000025#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +000026#include "malloc.h" /* for alloca */
Benjamin Peterson796798b2009-01-02 20:47:27 +000027#endif
Martin v. Löwis5344c992009-01-02 20:32:55 +000028
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;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000898 int set_file_name = 0, ret;
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 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000915 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000916 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000917 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000918 if (closeit)
919 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000920 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000921 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000922 ret = -1;
923 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000924 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000925 /* Turn on optimization if a .pyo file is given */
926 if (strcmp(ext, ".pyo") == 0)
927 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000928 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000929 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000930 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000931 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000932 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000933 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000934 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000935 ret = -1;
936 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000937 }
Guido van Rossum82598051997-03-05 00:20:32 +0000938 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000939 if (Py_FlushLine())
940 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000941 ret = 0;
942 done:
943 if (set_file_name && PyDict_DelItemString(d, "__file__"))
944 PyErr_Clear();
945 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000946}
947
948int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000949PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000950{
Guido van Rossum82598051997-03-05 00:20:32 +0000951 PyObject *m, *d, *v;
952 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000953 if (m == NULL)
954 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000955 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000956 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000958 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000959 return -1;
960 }
Guido van Rossum82598051997-03-05 00:20:32 +0000961 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000962 if (Py_FlushLine())
963 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964 return 0;
965}
966
Barry Warsaw035574d1997-08-29 22:07:17 +0000967static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000968parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
969 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000970{
971 long hold;
972 PyObject *v;
973
974 /* old style errors */
975 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000976 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000977 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000978
979 /* new style errors. `err' is an instance */
980
981 if (! (v = PyObject_GetAttrString(err, "msg")))
982 goto finally;
983 *message = v;
984
985 if (!(v = PyObject_GetAttrString(err, "filename")))
986 goto finally;
987 if (v == Py_None)
988 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000989 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000990 goto finally;
991
992 Py_DECREF(v);
993 if (!(v = PyObject_GetAttrString(err, "lineno")))
994 goto finally;
995 hold = PyInt_AsLong(v);
996 Py_DECREF(v);
997 v = NULL;
998 if (hold < 0 && PyErr_Occurred())
999 goto finally;
1000 *lineno = (int)hold;
1001
1002 if (!(v = PyObject_GetAttrString(err, "offset")))
1003 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001004 if (v == Py_None) {
1005 *offset = -1;
1006 Py_DECREF(v);
1007 v = NULL;
1008 } else {
1009 hold = PyInt_AsLong(v);
1010 Py_DECREF(v);
1011 v = NULL;
1012 if (hold < 0 && PyErr_Occurred())
1013 goto finally;
1014 *offset = (int)hold;
1015 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001016
1017 if (!(v = PyObject_GetAttrString(err, "text")))
1018 goto finally;
1019 if (v == Py_None)
1020 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001021 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001022 goto finally;
1023 Py_DECREF(v);
1024 return 1;
1025
1026finally:
1027 Py_XDECREF(v);
1028 return 0;
1029}
1030
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001031void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001032PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001033{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001034 PyErr_PrintEx(1);
1035}
1036
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001037static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001038print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001039{
1040 char *nl;
1041 if (offset >= 0) {
1042 if (offset > 0 && offset == (int)strlen(text))
1043 offset--;
1044 for (;;) {
1045 nl = strchr(text, '\n');
1046 if (nl == NULL || nl-text >= offset)
1047 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001049 text = nl+1;
1050 }
1051 while (*text == ' ' || *text == '\t') {
1052 text++;
1053 offset--;
1054 }
1055 }
1056 PyFile_WriteString(" ", f);
1057 PyFile_WriteString(text, f);
1058 if (*text == '\0' || text[strlen(text)-1] != '\n')
1059 PyFile_WriteString("\n", f);
1060 if (offset == -1)
1061 return;
1062 PyFile_WriteString(" ", f);
1063 offset--;
1064 while (offset > 0) {
1065 PyFile_WriteString(" ", f);
1066 offset--;
1067 }
1068 PyFile_WriteString("^\n", f);
1069}
1070
Guido van Rossum66e8e862001-03-23 17:54:43 +00001071static void
1072handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001073{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001074 PyObject *exception, *value, *tb;
1075 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001076
Georg Brandl49aafc92007-03-07 00:34:46 +00001077 if (Py_InspectFlag)
1078 /* Don't exit if -i flag was given. This flag is set to 0
1079 * when entering interactive mode for inspecting. */
1080 return;
1081
Guido van Rossum66e8e862001-03-23 17:54:43 +00001082 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001083 if (Py_FlushLine())
1084 PyErr_Clear();
1085 fflush(stdout);
1086 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001087 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001088 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001089 /* The error code should be in the `code' attribute. */
1090 PyObject *code = PyObject_GetAttrString(value, "code");
1091 if (code) {
1092 Py_DECREF(value);
1093 value = code;
1094 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001095 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001096 }
1097 /* If we failed to dig out the 'code' attribute,
1098 just let the else clause below print the error. */
1099 }
1100 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001101 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001102 else {
1103 PyObject_Print(value, stderr, Py_PRINT_RAW);
1104 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001105 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001106 }
Tim Peterscf615b52003-04-19 18:47:02 +00001107 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001108 /* Restore and clear the exception info, in order to properly decref
1109 * the exception, value, and traceback. If we just exit instead,
1110 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1111 * some finalizers from running.
1112 */
Tim Peterscf615b52003-04-19 18:47:02 +00001113 PyErr_Restore(exception, value, tb);
1114 PyErr_Clear();
1115 Py_Exit(exitcode);
1116 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001117}
1118
1119void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001120PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001121{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001122 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001123
1124 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1125 handle_system_exit();
1126 }
Guido van Rossum82598051997-03-05 00:20:32 +00001127 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 if (exception == NULL)
1129 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001130 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001131 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001132 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001133 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001134 if (set_sys_last_vars) {
1135 PySys_SetObject("last_type", exception);
1136 PySys_SetObject("last_value", v);
1137 PySys_SetObject("last_traceback", tb);
1138 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001139 hook = PySys_GetObject("excepthook");
1140 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001141 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001142 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001143 PyObject *result = PyEval_CallObject(hook, args);
1144 if (result == NULL) {
1145 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001146 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1147 handle_system_exit();
1148 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001149 PyErr_Fetch(&exception2, &v2, &tb2);
1150 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001151 /* It should not be possible for exception2 or v2
1152 to be NULL. However PyErr_Display() can't
1153 tolerate NULLs, so just be safe. */
1154 if (exception2 == NULL) {
1155 exception2 = Py_None;
1156 Py_INCREF(exception2);
1157 }
1158 if (v2 == NULL) {
1159 v2 = Py_None;
1160 Py_INCREF(v2);
1161 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001162 if (Py_FlushLine())
1163 PyErr_Clear();
1164 fflush(stdout);
1165 PySys_WriteStderr("Error in sys.excepthook:\n");
1166 PyErr_Display(exception2, v2, tb2);
1167 PySys_WriteStderr("\nOriginal exception was:\n");
1168 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001169 Py_DECREF(exception2);
1170 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001171 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001172 }
1173 Py_XDECREF(result);
1174 Py_XDECREF(args);
1175 } else {
1176 PySys_WriteStderr("sys.excepthook is missing\n");
1177 PyErr_Display(exception, v, tb);
1178 }
1179 Py_XDECREF(exception);
1180 Py_XDECREF(v);
1181 Py_XDECREF(tb);
1182}
1183
Richard Jones7b9558d2006-05-27 12:29:24 +00001184void
1185PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001186{
1187 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001188 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001189 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001190 if (f == NULL)
1191 fprintf(stderr, "lost sys.stderr\n");
1192 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001193 if (Py_FlushLine())
1194 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001195 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001196 if (tb && tb != Py_None)
1197 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001198 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001199 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001200 {
Guido van Rossum82598051997-03-05 00:20:32 +00001201 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001202 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001203 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001204 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001205 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001206 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001207 else {
1208 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001210 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001211 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001212 else
Guido van Rossum82598051997-03-05 00:20:32 +00001213 PyFile_WriteString(filename, f);
1214 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001215 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001216 PyFile_WriteString(buf, f);
1217 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001218 if (text != NULL)
1219 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001220 Py_DECREF(value);
1221 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001222 /* Can't be bothered to check all those
1223 PyFile_WriteString() calls */
1224 if (PyErr_Occurred())
1225 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001226 }
1227 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001228 if (err) {
1229 /* Don't do anything else */
1230 }
Brett Cannonbf364092006-03-01 04:25:17 +00001231 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001232 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001233 char* className = PyExceptionClass_Name(exception);
1234 if (className != NULL) {
1235 char *dot = strrchr(className, '.');
1236 if (dot != NULL)
1237 className = dot+1;
1238 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001239
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001240 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001241 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001242 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001243 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001244 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001245 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001246 {
1247 err = PyFile_WriteString(modstr, f);
1248 err += PyFile_WriteString(".", f);
1249 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001250 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001251 }
1252 if (err == 0) {
1253 if (className == NULL)
1254 err = PyFile_WriteString("<unknown>", f);
1255 else
Brett Cannonbf364092006-03-01 04:25:17 +00001256 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001257 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001258 }
1259 else
1260 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001261 if (err == 0 && (value != Py_None)) {
1262 PyObject *s = PyObject_Str(value);
1263 /* only print colon if the str() of the
1264 object is not the empty string
1265 */
1266 if (s == NULL)
1267 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001268 else if (!PyString_Check(s) ||
1269 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001270 err = PyFile_WriteString(": ", f);
1271 if (err == 0)
1272 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1273 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001274 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001275 /* try to write a newline in any case */
1276 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001277 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001278 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001279 /* If an error happened here, don't show it.
1280 XXX This is wrong, but too many callers rely on this behavior. */
1281 if (err != 0)
1282 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001283}
1284
Guido van Rossum82598051997-03-05 00:20:32 +00001285PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001286PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288{
Neal Norwitze92fba02006-03-04 18:52:26 +00001289 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001290 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001291 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001292 if (arena == NULL)
1293 return NULL;
1294
1295 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001296 if (mod != NULL)
1297 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001298 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001300}
1301
1302PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001303PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001304 PyObject *locals, int closeit, PyCompilerFlags *flags)
1305{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001307 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001308 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001309 if (arena == NULL)
1310 return NULL;
1311
1312 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1313 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001314 if (closeit)
1315 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001316 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001317 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001319 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001320 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001321 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001323}
1324
Guido van Rossum82598051997-03-05 00:20:32 +00001325static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001328{
Guido van Rossum82598051997-03-05 00:20:32 +00001329 PyCodeObject *co;
1330 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001331 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001332 if (co == NULL)
1333 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001334 v = PyEval_EvalCode(co, globals, locals);
1335 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001336 return v;
1337}
1338
Guido van Rossum82598051997-03-05 00:20:32 +00001339static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001340run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001342{
Guido van Rossum82598051997-03-05 00:20:32 +00001343 PyCodeObject *co;
1344 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001345 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001346 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001347
Guido van Rossum82598051997-03-05 00:20:32 +00001348 magic = PyMarshal_ReadLongFromFile(fp);
1349 if (magic != PyImport_GetMagicNumber()) {
1350 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001351 "Bad magic number in .pyc file");
1352 return NULL;
1353 }
Guido van Rossum82598051997-03-05 00:20:32 +00001354 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001355 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001356 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001357 if (v == NULL || !PyCode_Check(v)) {
1358 Py_XDECREF(v);
1359 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001360 "Bad code object in .pyc file");
1361 return NULL;
1362 }
Guido van Rossum82598051997-03-05 00:20:32 +00001363 co = (PyCodeObject *)v;
1364 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001365 if (v && flags)
1366 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001367 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001368 return v;
1369}
1370
Guido van Rossum82598051997-03-05 00:20:32 +00001371PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001372Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001373 PyCompilerFlags *flags)
1374{
Guido van Rossum82598051997-03-05 00:20:32 +00001375 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001376 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001377 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001378 if (arena == NULL)
1379 return NULL;
1380
1381 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001382 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001383 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001384 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001385 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001386 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001387 PyObject *result = PyAST_mod2obj(mod);
1388 PyArena_Free(arena);
1389 return result;
1390 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001391 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001392 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001393 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001394}
1395
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001396struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001397Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001398{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001399 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001400 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001401 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001402 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001403 if (arena == NULL)
1404 return NULL;
1405
Christian Heimes7f23d862008-03-26 22:51:58 +00001406 flags.cf_flags = 0;
1407
Christian Heimes3c608332008-03-26 22:01:37 +00001408 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001409 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001410 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001411 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001414 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001415 return st;
1416}
1417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418/* Preferred access to parser is through AST. */
1419mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001420PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001421 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001424 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001426 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001427
1428 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001429 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001430 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001431 if (flags == NULL) {
1432 localflags.cf_flags = 0;
1433 flags = &localflags;
1434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001436 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001437 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 PyNode_Free(n);
1439 return mod;
1440 }
1441 else {
1442 err_input(&err);
1443 return NULL;
1444 }
1445}
1446
1447mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001448PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001449 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001450 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001453 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001455 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001456
Christian Heimes3c608332008-03-26 22:01:37 +00001457 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1458 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001459 if (flags == NULL) {
1460 localflags.cf_flags = 0;
1461 flags = &localflags;
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001464 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001465 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 PyNode_Free(n);
1467 return mod;
1468 }
1469 else {
1470 err_input(&err);
1471 if (errcode)
1472 *errcode = err.error;
1473 return NULL;
1474 }
1475}
1476
Guido van Rossuma110aa61994-08-29 12:50:44 +00001477/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001478
Guido van Rossuma110aa61994-08-29 12:50:44 +00001479node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001480PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001481{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001482 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001483 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1484 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001485 if (n == NULL)
1486 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001487
Guido van Rossuma110aa61994-08-29 12:50:44 +00001488 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001489}
1490
Guido van Rossuma110aa61994-08-29 12:50:44 +00001491/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001492
Guido van Rossuma110aa61994-08-29 12:50:44 +00001493node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001494PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001495{
Tim Petersfe2127d2001-07-16 05:37:24 +00001496 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001497 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1498 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001499 if (n == NULL)
1500 err_input(&err);
1501 return n;
1502}
1503
1504node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001505PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001506 int start, int flags)
1507{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001508 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001509 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1510 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001511 if (n == NULL)
1512 err_input(&err);
1513 return n;
1514}
1515
1516node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001517PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001518{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001519 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001520}
1521
Guido van Rossum66ebd912003-04-17 16:02:26 +00001522/* May want to move a more generalized form of this to parsetok.c or
1523 even parser modules. */
1524
1525void
1526PyParser_SetError(perrdetail *err)
1527{
1528 err_input(err);
1529}
1530
Guido van Rossuma110aa61994-08-29 12:50:44 +00001531/* Set the error appropriate to the given input error code (see errcode.h) */
1532
1533static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001534err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001535{
Fred Drake85f36392000-07-11 17:53:00 +00001536 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001537 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001538 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001539 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001540 switch (err->error) {
1541 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001542 errtype = PyExc_IndentationError;
1543 if (err->expected == INDENT)
1544 msg = "expected an indented block";
1545 else if (err->token == INDENT)
1546 msg = "unexpected indent";
1547 else if (err->token == DEDENT)
1548 msg = "unexpected unindent";
1549 else {
1550 errtype = PyExc_SyntaxError;
1551 msg = "invalid syntax";
1552 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001553 break;
1554 case E_TOKEN:
1555 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001556 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001557 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001558 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001559 break;
1560 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001561 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001562 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001563 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001564 if (!PyErr_Occurred())
1565 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001566 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001567 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001568 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001569 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001570 case E_EOF:
1571 msg = "unexpected EOF while parsing";
1572 break;
Fred Drake85f36392000-07-11 17:53:00 +00001573 case E_TABSPACE:
1574 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001575 msg = "inconsistent use of tabs and spaces in indentation";
1576 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001577 case E_OVERFLOW:
1578 msg = "expression too long";
1579 break;
Fred Drake85f36392000-07-11 17:53:00 +00001580 case E_DEDENT:
1581 errtype = PyExc_IndentationError;
1582 msg = "unindent does not match any outer indentation level";
1583 break;
1584 case E_TOODEEP:
1585 errtype = PyExc_IndentationError;
1586 msg = "too many levels of indentation";
1587 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001588 case E_DECODE: {
1589 PyObject *type, *value, *tb;
1590 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001591 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001592 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001593 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001594 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001595 }
1596 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001597 if (msg == NULL)
1598 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001599 Py_XDECREF(type);
1600 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001601 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001602 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001603 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001604 case E_LINECONT:
1605 msg = "unexpected character after line continuation character";
1606 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001607 default:
1608 fprintf(stderr, "error=%d\n", err->error);
1609 msg = "unknown parsing error";
1610 break;
1611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 v = Py_BuildValue("(ziiz)", err->filename,
1613 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 w = NULL;
1615 if (v != NULL)
1616 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001617 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001618 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001619 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001620 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001621cleanup:
1622 if (err->text != NULL) {
1623 PyObject_FREE(err->text);
1624 err->text = NULL;
1625 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001626}
1627
1628/* Print fatal error message and abort */
1629
1630void
Tim Peters7c321a82002-07-09 02:57:01 +00001631Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001632{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001633 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001634#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +00001635 {
1636 size_t len = strlen(msg);
1637 WCHAR* buffer;
1638 size_t i;
1639
1640 /* Convert the message to wchar_t. This uses a simple one-to-one
1641 conversion, assuming that the this error message actually uses ASCII
1642 only. If this ceases to be true, we will have to convert. */
1643 buffer = alloca( (len+1) * (sizeof *buffer));
1644 for( i=0; i<=len; ++i)
1645 buffer[i] = msg[i];
1646 OutputDebugStringW(L"Fatal Python error: ");
1647 OutputDebugStringW(buffer);
1648 OutputDebugStringW(L"\n");
1649 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001650#ifdef _DEBUG
1651 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001652#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001653#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001654 abort();
1655}
1656
1657/* Clean up and exit */
1658
Guido van Rossuma110aa61994-08-29 12:50:44 +00001659#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001660#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001661#endif
1662
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001663#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001664static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001665static int nexitfuncs = 0;
1666
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001667int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001668{
1669 if (nexitfuncs >= NEXITFUNCS)
1670 return -1;
1671 exitfuncs[nexitfuncs++] = func;
1672 return 0;
1673}
1674
Guido van Rossumcc283f51997-08-05 02:22:03 +00001675static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001676call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001677{
Guido van Rossum82598051997-03-05 00:20:32 +00001678 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001679
1680 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001681 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001682 Py_INCREF(exitfunc);
1683 PySys_SetObject("exitfunc", (PyObject *)NULL);
1684 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001685 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001686 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1687 PySys_WriteStderr("Error in sys.exitfunc:\n");
1688 }
Guido van Rossum82598051997-03-05 00:20:32 +00001689 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001690 }
Guido van Rossum82598051997-03-05 00:20:32 +00001691 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001692 }
1693
Guido van Rossum0829c751998-02-28 04:31:39 +00001694 if (Py_FlushLine())
1695 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001696}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001697
Guido van Rossumcc283f51997-08-05 02:22:03 +00001698static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001699call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001700{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001701 while (nexitfuncs > 0)
1702 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001703
1704 fflush(stdout);
1705 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001706}
1707
1708void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001709Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001710{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001711 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001712
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001713 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001714}
1715
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001716static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001717initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001718{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001719#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001720 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001721#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001722#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001723 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001724#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001725#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001726 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001727#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001728 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001729}
1730
Guido van Rossum7433b121997-02-14 19:45:36 +00001731
1732/*
1733 * The file descriptor fd is considered ``interactive'' if either
1734 * a) isatty(fd) is TRUE, or
1735 * b) the -i flag was given, and the filename associated with
1736 * the descriptor is NULL or "<stdin>" or "???".
1737 */
1738int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001739Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001740{
1741 if (isatty((int)fileno(fp)))
1742 return 1;
1743 if (!Py_InteractiveFlag)
1744 return 0;
1745 return (filename == NULL) ||
1746 (strcmp(filename, "<stdin>") == 0) ||
1747 (strcmp(filename, "???") == 0);
1748}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001749
1750
Tim Petersd08e3822003-04-17 15:24:21 +00001751#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001752#if defined(WIN32) && defined(_MSC_VER)
1753
1754/* Stack checking for Microsoft C */
1755
1756#include <malloc.h>
1757#include <excpt.h>
1758
Fred Drakee8de31c2000-08-31 05:38:39 +00001759/*
1760 * Return non-zero when we run out of memory on the stack; zero otherwise.
1761 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001762int
Fred Drake399739f2000-08-31 05:52:44 +00001763PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001764{
1765 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001766 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001767 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001768 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001769 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001770 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1771 EXCEPTION_EXECUTE_HANDLER :
1772 EXCEPTION_CONTINUE_SEARCH) {
1773 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001774 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001775 {
1776 Py_FatalError("Could not reset the stack!");
1777 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001778 }
1779 return 1;
1780}
1781
1782#endif /* WIN32 && _MSC_VER */
1783
1784/* Alternate implementations can be added here... */
1785
1786#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001787
1788
1789/* Wrappers around sigaction() or signal(). */
1790
1791PyOS_sighandler_t
1792PyOS_getsig(int sig)
1793{
1794#ifdef HAVE_SIGACTION
1795 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001796 if (sigaction(sig, NULL, &context) == -1)
1797 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001798 return context.sa_handler;
1799#else
1800 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001801/* Special signal handling for the secure CRT in Visual Studio 2005 */
1802#if defined(_MSC_VER) && _MSC_VER >= 1400
1803 switch (sig) {
1804 /* Only these signals are valid */
1805 case SIGINT:
1806 case SIGILL:
1807 case SIGFPE:
1808 case SIGSEGV:
1809 case SIGTERM:
1810 case SIGBREAK:
1811 case SIGABRT:
1812 break;
1813 /* Don't call signal() with other values or it will assert */
1814 default:
1815 return SIG_ERR;
1816 }
1817#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001818 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001819 if (handler != SIG_ERR)
1820 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001821 return handler;
1822#endif
1823}
1824
1825PyOS_sighandler_t
1826PyOS_setsig(int sig, PyOS_sighandler_t handler)
1827{
1828#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001829 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001830 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001831 sigemptyset(&context.sa_mask);
1832 context.sa_flags = 0;
1833 if (sigaction(sig, &context, &ocontext) == -1)
1834 return SIG_ERR;
1835 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001836#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001837 PyOS_sighandler_t oldhandler;
1838 oldhandler = signal(sig, handler);
1839#ifdef HAVE_SIGINTERRUPT
1840 siginterrupt(sig, 1);
1841#endif
1842 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001843#endif
1844}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845
1846/* Deprecated C API functions still provided for binary compatiblity */
1847
1848#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001849PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1851{
1852 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1853}
1854
Thomas Heller1b046642006-04-18 18:51:06 +00001855#undef PyParser_SimpleParseString
1856PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857PyParser_SimpleParseString(const char *str, int start)
1858{
1859 return PyParser_SimpleParseStringFlags(str, start, 0);
1860}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001861
Thomas Heller1b046642006-04-18 18:51:06 +00001862#undef PyRun_AnyFile
1863PyAPI_FUNC(int)
1864PyRun_AnyFile(FILE *fp, const char *name)
1865{
1866 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1867}
1868
1869#undef PyRun_AnyFileEx
1870PyAPI_FUNC(int)
1871PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1872{
1873 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1874}
1875
1876#undef PyRun_AnyFileFlags
1877PyAPI_FUNC(int)
1878PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1879{
1880 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1881}
1882
1883#undef PyRun_File
1884PyAPI_FUNC(PyObject *)
1885PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1886{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001887 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001888}
1889
1890#undef PyRun_FileEx
1891PyAPI_FUNC(PyObject *)
1892PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1893{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001894 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001895}
1896
1897#undef PyRun_FileFlags
1898PyAPI_FUNC(PyObject *)
1899PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1900 PyCompilerFlags *flags)
1901{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001902 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001903}
1904
1905#undef PyRun_SimpleFile
1906PyAPI_FUNC(int)
1907PyRun_SimpleFile(FILE *f, const char *p)
1908{
1909 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1910}
1911
1912#undef PyRun_SimpleFileEx
1913PyAPI_FUNC(int)
1914PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1915{
1916 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1917}
1918
1919
1920#undef PyRun_String
1921PyAPI_FUNC(PyObject *)
1922PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1923{
1924 return PyRun_StringFlags(str, s, g, l, NULL);
1925}
1926
1927#undef PyRun_SimpleString
1928PyAPI_FUNC(int)
1929PyRun_SimpleString(const char *s)
1930{
1931 return PyRun_SimpleStringFlags(s, NULL);
1932}
1933
1934#undef Py_CompileString
1935PyAPI_FUNC(PyObject *)
1936Py_CompileString(const char *str, const char *p, int s)
1937{
1938 return Py_CompileStringFlags(str, p, s, NULL);
1939}
1940
1941#undef PyRun_InteractiveOne
1942PyAPI_FUNC(int)
1943PyRun_InteractiveOne(FILE *f, const char *p)
1944{
1945 return PyRun_InteractiveOneFlags(f, p, NULL);
1946}
1947
1948#undef PyRun_InteractiveLoop
1949PyAPI_FUNC(int)
1950PyRun_InteractiveLoop(FILE *f, const char *p)
1951{
1952 return PyRun_InteractiveLoopFlags(f, p, NULL);
1953}
1954
Anthony Baxterac6bd462006-04-13 02:06:09 +00001955#ifdef __cplusplus
1956}
1957#endif
1958