blob: b3866cebb88ce19b8e40cd99752a33fd48f64c88 [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
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000185 if (!_PyLong_Init())
186 Py_FatalError("Py_Initialize: can't init longs");
187
Christian Heimes3497f942008-05-26 12:29:14 +0000188 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000189 Py_FatalError("Py_Initialize: can't init bytearray");
190
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000191 _PyFloat_Init();
192
Guido van Rossum25ce5661997-08-02 03:10:38 +0000193 interp->modules = PyDict_New();
194 if (interp->modules == NULL)
195 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000196 interp->modules_reloading = PyDict_New();
197 if (interp->modules_reloading == NULL)
198 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000199
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000200#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000201 /* Init Unicode implementation; relies on the codec registry */
202 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000203#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000204
Barry Warsawf242aa02000-05-25 23:09:49 +0000205 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000206 if (bimod == NULL)
207 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000208 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000209 if (interp->builtins == NULL)
210 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000211 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212
213 sysmod = _PySys_Init();
214 if (sysmod == NULL)
215 Py_FatalError("Py_Initialize: can't initialize sys");
216 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000217 if (interp->sysdict == NULL)
218 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219 Py_INCREF(interp->sysdict);
220 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000221 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000222 PyDict_SetItemString(interp->sysdict, "modules",
223 interp->modules);
224
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000225 _PyImport_Init();
226
Barry Warsawf242aa02000-05-25 23:09:49 +0000227 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000228 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000229 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000230
Barry Warsaw035574d1997-08-29 22:07:17 +0000231 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000232 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000233
Just van Rossum52e14d62002-12-30 22:08:05 +0000234 _PyImportHooks_Init();
235
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000236 if (install_sigs)
237 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000238
Georg Brandl3c0fd562008-07-05 10:07:18 +0000239 /* Initialize warnings. */
240 _PyWarnings_Init();
241 if (PySys_HasWarnOptions()) {
242 PyObject *warnings_module = PyImport_ImportModule("warnings");
243 if (!warnings_module)
244 PyErr_Clear();
245 Py_XDECREF(warnings_module);
246 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247
248 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000249 if (!Py_NoSiteFlag)
250 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000251
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000252 /* auto-thread-state API, if available */
253#ifdef WITH_THREAD
254 _PyGILState_Init(interp, tstate);
255#endif /* WITH_THREAD */
256
Martin v. Löwis99815892008-06-01 07:20:46 +0000257 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
258 p = icodeset = codeset = strdup(p);
259 free_codeset = 1;
260 errors = strchr(p, ':');
261 if (errors) {
262 *errors = '\0';
263 errors++;
264 }
265 overridden = 1;
266 }
267
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000268#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
269 /* On Unix, set the file system encoding according to the
270 user's preference, if the CODESET names a well-known
271 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000272 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000273 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274
Martin v. Löwis99815892008-06-01 07:20:46 +0000275 if (!overridden || !Py_FileSystemDefaultEncoding) {
276 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
277 setlocale(LC_CTYPE, "");
278 loc_codeset = nl_langinfo(CODESET);
279 if (loc_codeset && *loc_codeset) {
280 PyObject *enc = PyCodec_Encoder(loc_codeset);
281 if (enc) {
282 loc_codeset = strdup(loc_codeset);
283 Py_DECREF(enc);
284 } else {
285 loc_codeset = NULL;
286 PyErr_Clear();
287 }
288 } else
289 loc_codeset = NULL;
290 setlocale(LC_CTYPE, saved_locale);
291 free(saved_locale);
292
293 if (!overridden) {
294 codeset = icodeset = loc_codeset;
295 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000296 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000297
298 /* Initialize Py_FileSystemDefaultEncoding from
299 locale even if PYTHONIOENCODING is set. */
300 if (!Py_FileSystemDefaultEncoding) {
301 Py_FileSystemDefaultEncoding = loc_codeset;
302 if (!overridden)
303 free_codeset = 0;
304 }
305 }
306#endif
307
308#ifdef MS_WINDOWS
309 if (!overridden) {
310 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000311 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000312 sprintf(ibuf, "cp%d", GetConsoleCP());
313 sprintf(buf, "cp%d", GetConsoleOutputCP());
314 }
315#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000316
317 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000318 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000319 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
320 if (!sys_isatty)
321 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000322 if ((overridden ||
323 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000324 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000325 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000326 Py_FatalError("Cannot set codeset of stdin");
327 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000328 Py_XDECREF(sys_isatty);
329
330 sys_stream = PySys_GetObject("stdout");
331 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
332 if (!sys_isatty)
333 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000334 if ((overridden ||
335 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000336 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000337 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000338 Py_FatalError("Cannot set codeset of stdout");
339 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000340 Py_XDECREF(sys_isatty);
341
Martin v. Löwisea62d252006-04-03 10:56:49 +0000342 sys_stream = PySys_GetObject("stderr");
343 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
344 if (!sys_isatty)
345 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000346 if((overridden ||
347 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000348 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000349 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000350 Py_FatalError("Cannot set codeset of stderr");
351 }
352 Py_XDECREF(sys_isatty);
353
Martin v. Löwis99815892008-06-01 07:20:46 +0000354 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000355 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000356 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000357}
358
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000359void
360Py_Initialize(void)
361{
362 Py_InitializeEx(1);
363}
364
365
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000366#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000367extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000368#endif
369
Guido van Rossum25ce5661997-08-02 03:10:38 +0000370/* Undo the effect of Py_Initialize().
371
372 Beware: if multiple interpreter and/or thread states exist, these
373 are not wiped out; only the current thread and interpreter state
374 are deleted. But since everything else is deleted, those other
375 interpreter and thread states should no longer be used.
376
377 (XXX We should do better, e.g. wipe out all interpreters and
378 threads.)
379
380 Locking: as above.
381
382*/
383
384void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000385Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000386{
387 PyInterpreterState *interp;
388 PyThreadState *tstate;
389
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000390 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000391 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000392
Tim Peters384fd102001-01-21 03:40:37 +0000393 /* The interpreter is still entirely intact at this point, and the
394 * exit funcs may be relying on that. In particular, if some thread
395 * or exit func is still waiting to do an import, the import machinery
396 * expects Py_IsInitialized() to return true. So don't say the
397 * interpreter is uninitialized until after the exit funcs have run.
398 * Note that Threading.py uses an exit func to do a join on all the
399 * threads created thru it, so this also protects pending imports in
400 * the threads created via Threading.
401 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000402 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000403 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000404
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000405 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000406 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000407 interp = tstate->interp;
408
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000409 /* Disable signal handling */
410 PyOS_FiniInterrupts();
411
Christian Heimes908caac2008-01-27 23:34:59 +0000412 /* Clear type lookup cache */
413 PyType_ClearCache();
414
Guido van Rossume13ddc92003-04-17 17:29:22 +0000415 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000416 * before all modules are destroyed.
417 * XXX If a __del__ or weakref callback is triggered here, and tries to
418 * XXX import a module, bad things can happen, because Python no
419 * XXX longer believes it's initialized.
420 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
421 * XXX is easy to provoke that way. I've also seen, e.g.,
422 * XXX Exception exceptions.ImportError: 'No module named sha'
423 * XXX in <function callback at 0x008F5718> ignored
424 * XXX but I'm unclear on exactly how that one happens. In any case,
425 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000426 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000427 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000428#ifdef COUNT_ALLOCS
429 /* With COUNT_ALLOCS, it helps to run GC multiple times:
430 each collection might release some types from the type
431 list, so they become garbage. */
432 while (PyGC_Collect() > 0)
433 /* nothing */;
434#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000435
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000436 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000437 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000438
Guido van Rossume13ddc92003-04-17 17:29:22 +0000439 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000440 * new-style class definitions, for example.
441 * XXX This is disabled because it caused too many problems. If
442 * XXX a __del__ or weakref callback triggers here, Python code has
443 * XXX a hard time running, because even the sys module has been
444 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
445 * XXX One symptom is a sequence of information-free messages
446 * XXX coming from threads (if a __del__ or callback is invoked,
447 * XXX other threads can execute too, and any exception they encounter
448 * XXX triggers a comedy of errors as subsystem after subsystem
449 * XXX fails to find what it *expects* to find in sys to help report
450 * XXX the exception and consequent unexpected failures). I've also
451 * XXX seen segfaults then, after adding print statements to the
452 * XXX Python code getting called.
453 */
454#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000455 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000456#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000457
Guido van Rossum1707aad1997-12-08 23:43:45 +0000458 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
459 _PyImport_Fini();
460
461 /* Debugging stuff */
462#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000463 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000464#endif
465
Tim Peters62e97f02006-03-28 21:44:32 +0000466 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000467
Tim Peters9cf25ce2003-04-17 15:21:01 +0000468#ifdef Py_TRACE_REFS
469 /* Display all objects still alive -- this can invoke arbitrary
470 * __repr__ overrides, so requires a mostly-intact interpreter.
471 * Alas, a lot of stuff may still be alive now that will be cleaned
472 * up later.
473 */
Tim Peters269b2a62003-04-17 19:52:29 +0000474 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000475 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000476#endif /* Py_TRACE_REFS */
477
Guido van Rossumd922fa42003-04-15 14:10:09 +0000478 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000479 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000480
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000481 /* Now we decref the exception classes. After this point nothing
482 can raise an exception. That's okay, because each Fini() method
483 below has been checked to make sure no exceptions are ever
484 raised.
485 */
486
487 _PyExc_Fini();
488
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000489 /* Cleanup auto-thread-state */
490#ifdef WITH_THREAD
491 _PyGILState_Fini();
492#endif /* WITH_THREAD */
493
Guido van Rossumd922fa42003-04-15 14:10:09 +0000494 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000495 PyThreadState_Swap(NULL);
496 PyInterpreterState_Delete(interp);
497
Guido van Rossumd922fa42003-04-15 14:10:09 +0000498 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000499 PyMethod_Fini();
500 PyFrame_Fini();
501 PyCFunction_Fini();
502 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000503 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000504 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000505 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000506 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000507 PyInt_Fini();
508 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000509 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000510
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000511#ifdef Py_USING_UNICODE
512 /* Cleanup Unicode implementation */
513 _PyUnicode_Fini();
514#endif
515
Guido van Rossumcc283f51997-08-05 02:22:03 +0000516 /* XXX Still allocated:
517 - various static ad-hoc pointers to interned strings
518 - int and float free list blocks
519 - whatever various modules and libraries allocate
520 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000521
522 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000523
Tim Peters269b2a62003-04-17 19:52:29 +0000524#ifdef Py_TRACE_REFS
525 /* Display addresses (& refcnts) of all objects still alive.
526 * An address can be used to find the repr of the object, printed
527 * above by _Py_PrintReferences.
528 */
529 if (Py_GETENV("PYTHONDUMPREFS"))
530 _Py_PrintReferenceAddresses(stderr);
531#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000532#ifdef PYMALLOC_DEBUG
533 if (Py_GETENV("PYTHONMALLOCSTATS"))
534 _PyObject_DebugMallocStats();
535#endif
536
Guido van Rossumcc283f51997-08-05 02:22:03 +0000537 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000538}
539
540/* Create and initialize a new interpreter and thread, and return the
541 new thread. This requires that Py_Initialize() has been called
542 first.
543
544 Unsuccessful initialization yields a NULL pointer. Note that *no*
545 exception information is available even in this case -- the
546 exception information is held in the thread, and there is no
547 thread.
548
549 Locking: as above.
550
551*/
552
553PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000554Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000555{
556 PyInterpreterState *interp;
557 PyThreadState *tstate, *save_tstate;
558 PyObject *bimod, *sysmod;
559
560 if (!initialized)
561 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
562
563 interp = PyInterpreterState_New();
564 if (interp == NULL)
565 return NULL;
566
567 tstate = PyThreadState_New(interp);
568 if (tstate == NULL) {
569 PyInterpreterState_Delete(interp);
570 return NULL;
571 }
572
573 save_tstate = PyThreadState_Swap(tstate);
574
575 /* XXX The following is lax in error checking */
576
577 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000578 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000579
580 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
581 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000582 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000583 if (interp->builtins == NULL)
584 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000585 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000586 }
587 sysmod = _PyImport_FindExtension("sys", "sys");
588 if (bimod != NULL && sysmod != NULL) {
589 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000590 if (interp->sysdict == NULL)
591 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 Py_INCREF(interp->sysdict);
593 PySys_SetPath(Py_GetPath());
594 PyDict_SetItemString(interp->sysdict, "modules",
595 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000596 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000597 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000598 if (!Py_NoSiteFlag)
599 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000600 }
601
602 if (!PyErr_Occurred())
603 return tstate;
604
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000605handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000606 /* Oops, it didn't work. Undo it all. */
607
608 PyErr_Print();
609 PyThreadState_Clear(tstate);
610 PyThreadState_Swap(save_tstate);
611 PyThreadState_Delete(tstate);
612 PyInterpreterState_Delete(interp);
613
614 return NULL;
615}
616
617/* Delete an interpreter and its last thread. This requires that the
618 given thread state is current, that the thread has no remaining
619 frames, and that it is its interpreter's only remaining thread.
620 It is a fatal error to violate these constraints.
621
622 (Py_Finalize() doesn't have these constraints -- it zaps
623 everything, regardless.)
624
625 Locking: as above.
626
627*/
628
629void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000630Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000631{
632 PyInterpreterState *interp = tstate->interp;
633
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000634 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000635 Py_FatalError("Py_EndInterpreter: thread is not current");
636 if (tstate->frame != NULL)
637 Py_FatalError("Py_EndInterpreter: thread still has a frame");
638 if (tstate != interp->tstate_head || tstate->next != NULL)
639 Py_FatalError("Py_EndInterpreter: not the last thread");
640
641 PyImport_Cleanup();
642 PyInterpreterState_Clear(interp);
643 PyThreadState_Swap(NULL);
644 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000645}
646
647static char *progname = "python";
648
649void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000651{
652 if (pn && *pn)
653 progname = pn;
654}
655
656char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000657Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000658{
659 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000660}
661
Guido van Rossuma61691e1998-02-06 22:27:24 +0000662static char *default_home = NULL;
663
664void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000665Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000666{
667 default_home = home;
668}
669
670char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000671Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000672{
673 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000674 if (home == NULL && !Py_IgnoreEnvironmentFlag)
675 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000676 return home;
677}
678
Guido van Rossum6135a871995-01-09 17:53:26 +0000679/* Create __main__ module */
680
681static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000682initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000683{
Guido van Rossum82598051997-03-05 00:20:32 +0000684 PyObject *m, *d;
685 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000686 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000687 Py_FatalError("can't create __main__ module");
688 d = PyModule_GetDict(m);
689 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000690 PyObject *bimod = PyImport_ImportModule("__builtin__");
691 if (bimod == NULL ||
692 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000693 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000694 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000695 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000696}
697
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000698/* Import the site module (not into __main__ though) */
699
700static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000701initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000702{
703 PyObject *m, *f;
704 m = PyImport_ImportModule("site");
705 if (m == NULL) {
706 f = PySys_GetObject("stderr");
707 if (Py_VerboseFlag) {
708 PyFile_WriteString(
709 "'import site' failed; traceback:\n", f);
710 PyErr_Print();
711 }
712 else {
713 PyFile_WriteString(
714 "'import site' failed; use -v for traceback\n", f);
715 PyErr_Clear();
716 }
717 }
718 else {
719 Py_DECREF(m);
720 }
721}
722
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000723/* Parse input from a file and execute it */
724
725int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000726PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000727 PyCompilerFlags *flags)
728{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000729 if (filename == NULL)
730 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000731 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000732 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000733 if (closeit)
734 fclose(fp);
735 return err;
736 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000737 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000738 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000739}
740
741int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000742PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000743{
Guido van Rossum82598051997-03-05 00:20:32 +0000744 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000745 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000746 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000747
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000748 if (flags == NULL) {
749 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000750 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000751 }
Guido van Rossum82598051997-03-05 00:20:32 +0000752 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000753 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000754 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000755 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000756 }
Guido van Rossum82598051997-03-05 00:20:32 +0000757 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000759 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000760 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000761 }
762 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000763 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000764 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000765 if (ret == E_EOF)
766 return 0;
767 /*
768 if (ret == E_NOMEM)
769 return -1;
770 */
771 }
772}
773
Eric Smith7c478942008-03-18 23:45:49 +0000774#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000775/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000776#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000777 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000778 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000779#endif
780#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000781/* Keep an example of flags with future keyword support. */
782#define PARSER_FLAGS(flags) \
783 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000784 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000785 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
786 PyPARSE_PRINT_IS_FUNCTION : 0) \
787 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
788 PyPARSE_UNICODE_LITERALS : 0) \
789 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000790#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000791
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000792int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000793PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000794{
Guido van Rossum82598051997-03-05 00:20:32 +0000795 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000797 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000798 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000800
Guido van Rossum82598051997-03-05 00:20:32 +0000801 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000802 if (v != NULL) {
803 v = PyObject_Str(v);
804 if (v == NULL)
805 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000806 else if (PyString_Check(v))
807 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000808 }
Guido van Rossum82598051997-03-05 00:20:32 +0000809 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000810 if (w != NULL) {
811 w = PyObject_Str(w);
812 if (w == NULL)
813 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000814 else if (PyString_Check(w))
815 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000816 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000817 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000818 if (arena == NULL) {
819 Py_XDECREF(v);
820 Py_XDECREF(w);
821 return -1;
822 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000823 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000825 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000826 Py_XDECREF(v);
827 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000829 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (errcode == E_EOF) {
831 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000832 return E_EOF;
833 }
Guido van Rossum82598051997-03-05 00:20:32 +0000834 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000836 }
Guido van Rossum82598051997-03-05 00:20:32 +0000837 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000838 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000839 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000841 }
Guido van Rossum82598051997-03-05 00:20:32 +0000842 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000843 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000844 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000845 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000846 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 return -1;
848 }
Guido van Rossum82598051997-03-05 00:20:32 +0000849 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000850 if (Py_FlushLine())
851 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000852 return 0;
853}
854
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000855/* Check whether a file maybe a pyc file: Look at the extension,
856 the file type, and, if we may close it, at the first few bytes. */
857
858static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000859maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000860{
861 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
862 return 1;
863
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000864 /* Only look into the file if we are allowed to close it, since
865 it then should also be seekable. */
866 if (closeit) {
867 /* Read only two bytes of the magic. If the file was opened in
868 text mode, the bytes 3 and 4 of the magic (\r\n) might not
869 be read as they are on disk. */
870 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
871 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000872 /* Mess: In case of -x, the stream is NOT at its start now,
873 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000874 which makes the current stream position formally undefined,
875 and a x-platform nightmare.
876 Unfortunately, we have no direct way to know whether -x
877 was specified. So we use a terrible hack: if the current
878 stream position is not 0, we assume -x was specified, and
879 give up. Bug 132850 on SourceForge spells out the
880 hopelessness of trying anything else (fseek and ftell
881 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000882 */
Tim Peters3e876562001-02-11 04:35:39 +0000883 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000884 if (ftell(fp) == 0) {
885 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000886 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000887 ispyc = 1;
888 rewind(fp);
889 }
Tim Peters3e876562001-02-11 04:35:39 +0000890 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000891 }
892 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000893}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000894
Guido van Rossum0df002c2000-08-27 19:21:52 +0000895int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000896PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000897 PyCompilerFlags *flags)
898{
Guido van Rossum82598051997-03-05 00:20:32 +0000899 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000900 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000901 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000902
Guido van Rossum82598051997-03-05 00:20:32 +0000903 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000904 if (m == NULL)
905 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000906 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000907 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000908 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000909 if (f == NULL)
910 return -1;
911 if (PyDict_SetItemString(d, "__file__", f) < 0) {
912 Py_DECREF(f);
913 return -1;
914 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000915 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000916 Py_DECREF(f);
917 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000918 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000919 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000920 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000921 if (closeit)
922 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000923 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000924 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000925 ret = -1;
926 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000927 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000928 /* Turn on optimization if a .pyo file is given */
929 if (strcmp(ext, ".pyo") == 0)
930 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000931 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000932 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000933 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000934 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000935 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000936 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000937 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000938 ret = -1;
939 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000940 }
Guido van Rossum82598051997-03-05 00:20:32 +0000941 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000942 if (Py_FlushLine())
943 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000944 ret = 0;
945 done:
946 if (set_file_name && PyDict_DelItemString(d, "__file__"))
947 PyErr_Clear();
948 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000949}
950
951int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000952PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000953{
Guido van Rossum82598051997-03-05 00:20:32 +0000954 PyObject *m, *d, *v;
955 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000956 if (m == NULL)
957 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000958 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000959 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000960 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000961 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000962 return -1;
963 }
Guido van Rossum82598051997-03-05 00:20:32 +0000964 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000965 if (Py_FlushLine())
966 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000967 return 0;
968}
969
Barry Warsaw035574d1997-08-29 22:07:17 +0000970static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000971parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
972 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000973{
974 long hold;
975 PyObject *v;
976
977 /* old style errors */
978 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000979 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000980 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000981
982 /* new style errors. `err' is an instance */
983
984 if (! (v = PyObject_GetAttrString(err, "msg")))
985 goto finally;
986 *message = v;
987
988 if (!(v = PyObject_GetAttrString(err, "filename")))
989 goto finally;
990 if (v == Py_None)
991 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000992 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000993 goto finally;
994
995 Py_DECREF(v);
996 if (!(v = PyObject_GetAttrString(err, "lineno")))
997 goto finally;
998 hold = PyInt_AsLong(v);
999 Py_DECREF(v);
1000 v = NULL;
1001 if (hold < 0 && PyErr_Occurred())
1002 goto finally;
1003 *lineno = (int)hold;
1004
1005 if (!(v = PyObject_GetAttrString(err, "offset")))
1006 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001007 if (v == Py_None) {
1008 *offset = -1;
1009 Py_DECREF(v);
1010 v = NULL;
1011 } else {
1012 hold = PyInt_AsLong(v);
1013 Py_DECREF(v);
1014 v = NULL;
1015 if (hold < 0 && PyErr_Occurred())
1016 goto finally;
1017 *offset = (int)hold;
1018 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001019
1020 if (!(v = PyObject_GetAttrString(err, "text")))
1021 goto finally;
1022 if (v == Py_None)
1023 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001024 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001025 goto finally;
1026 Py_DECREF(v);
1027 return 1;
1028
1029finally:
1030 Py_XDECREF(v);
1031 return 0;
1032}
1033
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001034void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001035PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001037 PyErr_PrintEx(1);
1038}
1039
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001040static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001041print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001042{
1043 char *nl;
1044 if (offset >= 0) {
1045 if (offset > 0 && offset == (int)strlen(text))
1046 offset--;
1047 for (;;) {
1048 nl = strchr(text, '\n');
1049 if (nl == NULL || nl-text >= offset)
1050 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001051 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001052 text = nl+1;
1053 }
1054 while (*text == ' ' || *text == '\t') {
1055 text++;
1056 offset--;
1057 }
1058 }
1059 PyFile_WriteString(" ", f);
1060 PyFile_WriteString(text, f);
1061 if (*text == '\0' || text[strlen(text)-1] != '\n')
1062 PyFile_WriteString("\n", f);
1063 if (offset == -1)
1064 return;
1065 PyFile_WriteString(" ", f);
1066 offset--;
1067 while (offset > 0) {
1068 PyFile_WriteString(" ", f);
1069 offset--;
1070 }
1071 PyFile_WriteString("^\n", f);
1072}
1073
Guido van Rossum66e8e862001-03-23 17:54:43 +00001074static void
1075handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001076{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001077 PyObject *exception, *value, *tb;
1078 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001079
Georg Brandl49aafc92007-03-07 00:34:46 +00001080 if (Py_InspectFlag)
1081 /* Don't exit if -i flag was given. This flag is set to 0
1082 * when entering interactive mode for inspecting. */
1083 return;
1084
Guido van Rossum66e8e862001-03-23 17:54:43 +00001085 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001086 if (Py_FlushLine())
1087 PyErr_Clear();
1088 fflush(stdout);
1089 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001090 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001091 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001092 /* The error code should be in the `code' attribute. */
1093 PyObject *code = PyObject_GetAttrString(value, "code");
1094 if (code) {
1095 Py_DECREF(value);
1096 value = code;
1097 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001098 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001099 }
1100 /* If we failed to dig out the 'code' attribute,
1101 just let the else clause below print the error. */
1102 }
1103 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001104 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001105 else {
1106 PyObject_Print(value, stderr, Py_PRINT_RAW);
1107 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001108 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001109 }
Tim Peterscf615b52003-04-19 18:47:02 +00001110 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001111 /* Restore and clear the exception info, in order to properly decref
1112 * the exception, value, and traceback. If we just exit instead,
1113 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1114 * some finalizers from running.
1115 */
Tim Peterscf615b52003-04-19 18:47:02 +00001116 PyErr_Restore(exception, value, tb);
1117 PyErr_Clear();
1118 Py_Exit(exitcode);
1119 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001120}
1121
1122void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001123PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001124{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001125 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001126
1127 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1128 handle_system_exit();
1129 }
Guido van Rossum82598051997-03-05 00:20:32 +00001130 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 if (exception == NULL)
1132 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001133 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001134 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001135 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001136 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001137 if (set_sys_last_vars) {
1138 PySys_SetObject("last_type", exception);
1139 PySys_SetObject("last_value", v);
1140 PySys_SetObject("last_traceback", tb);
1141 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001142 hook = PySys_GetObject("excepthook");
1143 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001144 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001145 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001146 PyObject *result = PyEval_CallObject(hook, args);
1147 if (result == NULL) {
1148 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001149 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1150 handle_system_exit();
1151 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001152 PyErr_Fetch(&exception2, &v2, &tb2);
1153 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001154 /* It should not be possible for exception2 or v2
1155 to be NULL. However PyErr_Display() can't
1156 tolerate NULLs, so just be safe. */
1157 if (exception2 == NULL) {
1158 exception2 = Py_None;
1159 Py_INCREF(exception2);
1160 }
1161 if (v2 == NULL) {
1162 v2 = Py_None;
1163 Py_INCREF(v2);
1164 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001165 if (Py_FlushLine())
1166 PyErr_Clear();
1167 fflush(stdout);
1168 PySys_WriteStderr("Error in sys.excepthook:\n");
1169 PyErr_Display(exception2, v2, tb2);
1170 PySys_WriteStderr("\nOriginal exception was:\n");
1171 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001172 Py_DECREF(exception2);
1173 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001174 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001175 }
1176 Py_XDECREF(result);
1177 Py_XDECREF(args);
1178 } else {
1179 PySys_WriteStderr("sys.excepthook is missing\n");
1180 PyErr_Display(exception, v, tb);
1181 }
1182 Py_XDECREF(exception);
1183 Py_XDECREF(v);
1184 Py_XDECREF(tb);
1185}
1186
Richard Jones7b9558d2006-05-27 12:29:24 +00001187void
1188PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001189{
1190 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001191 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001192 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001193 if (f == NULL)
1194 fprintf(stderr, "lost sys.stderr\n");
1195 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001196 if (Py_FlushLine())
1197 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001198 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001199 if (tb && tb != Py_None)
1200 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001201 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001202 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001203 {
Guido van Rossum82598051997-03-05 00:20:32 +00001204 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001205 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001206 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001207 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001208 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001210 else {
1211 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001212 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001213 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001214 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001215 else
Guido van Rossum82598051997-03-05 00:20:32 +00001216 PyFile_WriteString(filename, f);
1217 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001218 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001219 PyFile_WriteString(buf, f);
1220 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001221 if (text != NULL)
1222 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001223 Py_DECREF(value);
1224 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001225 /* Can't be bothered to check all those
1226 PyFile_WriteString() calls */
1227 if (PyErr_Occurred())
1228 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001229 }
1230 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001231 if (err) {
1232 /* Don't do anything else */
1233 }
Brett Cannonbf364092006-03-01 04:25:17 +00001234 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001235 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001236 char* className = PyExceptionClass_Name(exception);
1237 if (className != NULL) {
1238 char *dot = strrchr(className, '.');
1239 if (dot != NULL)
1240 className = dot+1;
1241 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001242
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001243 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001244 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001245 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001246 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001247 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001248 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001249 {
1250 err = PyFile_WriteString(modstr, f);
1251 err += PyFile_WriteString(".", f);
1252 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001253 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001254 }
1255 if (err == 0) {
1256 if (className == NULL)
1257 err = PyFile_WriteString("<unknown>", f);
1258 else
Brett Cannonbf364092006-03-01 04:25:17 +00001259 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001260 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001261 }
1262 else
1263 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001264 if (err == 0 && (value != Py_None)) {
1265 PyObject *s = PyObject_Str(value);
1266 /* only print colon if the str() of the
1267 object is not the empty string
1268 */
1269 if (s == NULL)
1270 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001271 else if (!PyString_Check(s) ||
1272 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001273 err = PyFile_WriteString(": ", f);
1274 if (err == 0)
1275 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1276 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001277 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001278 /* try to write a newline in any case */
1279 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001280 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001281 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001282 /* If an error happened here, don't show it.
1283 XXX This is wrong, but too many callers rely on this behavior. */
1284 if (err != 0)
1285 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286}
1287
Guido van Rossum82598051997-03-05 00:20:32 +00001288PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001289PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001291{
Neal Norwitze92fba02006-03-04 18:52:26 +00001292 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001293 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001294 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001295 if (arena == NULL)
1296 return NULL;
1297
1298 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001299 if (mod != NULL)
1300 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001301 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001303}
1304
1305PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001306PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001307 PyObject *locals, int closeit, PyCompilerFlags *flags)
1308{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001310 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001311 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001312 if (arena == NULL)
1313 return NULL;
1314
1315 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1316 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001317 if (closeit)
1318 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001319 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001320 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001322 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001323 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001324 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001326}
1327
Guido van Rossum82598051997-03-05 00:20:32 +00001328static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001330 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001331{
Guido van Rossum82598051997-03-05 00:20:32 +00001332 PyCodeObject *co;
1333 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001334 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001335 if (co == NULL)
1336 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001337 v = PyEval_EvalCode(co, globals, locals);
1338 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001339 return v;
1340}
1341
Guido van Rossum82598051997-03-05 00:20:32 +00001342static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001343run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001345{
Guido van Rossum82598051997-03-05 00:20:32 +00001346 PyCodeObject *co;
1347 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001348 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001349 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001350
Guido van Rossum82598051997-03-05 00:20:32 +00001351 magic = PyMarshal_ReadLongFromFile(fp);
1352 if (magic != PyImport_GetMagicNumber()) {
1353 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001354 "Bad magic number in .pyc file");
1355 return NULL;
1356 }
Guido van Rossum82598051997-03-05 00:20:32 +00001357 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001358 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001359 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001360 if (v == NULL || !PyCode_Check(v)) {
1361 Py_XDECREF(v);
1362 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001363 "Bad code object in .pyc file");
1364 return NULL;
1365 }
Guido van Rossum82598051997-03-05 00:20:32 +00001366 co = (PyCodeObject *)v;
1367 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001368 if (v && flags)
1369 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001370 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001371 return v;
1372}
1373
Guido van Rossum82598051997-03-05 00:20:32 +00001374PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001375Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001376 PyCompilerFlags *flags)
1377{
Guido van Rossum82598051997-03-05 00:20:32 +00001378 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001379 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001380 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001381 if (arena == NULL)
1382 return NULL;
1383
1384 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001385 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001386 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001387 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001388 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001389 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001390 PyObject *result = PyAST_mod2obj(mod);
1391 PyArena_Free(arena);
1392 return result;
1393 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001394 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001395 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001396 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001397}
1398
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001399struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001400Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001401{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001402 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001403 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001404 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001405 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001406 if (arena == NULL)
1407 return NULL;
1408
Christian Heimes7f23d862008-03-26 22:51:58 +00001409 flags.cf_flags = 0;
1410
Christian Heimes3c608332008-03-26 22:01:37 +00001411 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001412 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001413 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001414 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001415 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001417 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001418 return st;
1419}
1420
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421/* Preferred access to parser is through AST. */
1422mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001423PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001424 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001427 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001429 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001430
1431 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001432 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001433 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001434 if (flags == NULL) {
1435 localflags.cf_flags = 0;
1436 flags = &localflags;
1437 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001439 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001440 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 PyNode_Free(n);
1442 return mod;
1443 }
1444 else {
1445 err_input(&err);
1446 return NULL;
1447 }
1448}
1449
1450mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001451PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001452 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001453 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001456 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001458 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001459
Christian Heimes3c608332008-03-26 22:01:37 +00001460 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1461 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001462 if (flags == NULL) {
1463 localflags.cf_flags = 0;
1464 flags = &localflags;
1465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001467 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001468 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 PyNode_Free(n);
1470 return mod;
1471 }
1472 else {
1473 err_input(&err);
1474 if (errcode)
1475 *errcode = err.error;
1476 return NULL;
1477 }
1478}
1479
Guido van Rossuma110aa61994-08-29 12:50:44 +00001480/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001481
Guido van Rossuma110aa61994-08-29 12:50:44 +00001482node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001483PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001484{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001485 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001486 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1487 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001488 if (n == NULL)
1489 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001490
Guido van Rossuma110aa61994-08-29 12:50:44 +00001491 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001492}
1493
Guido van Rossuma110aa61994-08-29 12:50:44 +00001494/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001495
Guido van Rossuma110aa61994-08-29 12:50:44 +00001496node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001497PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001498{
Tim Petersfe2127d2001-07-16 05:37:24 +00001499 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001500 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1501 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001502 if (n == NULL)
1503 err_input(&err);
1504 return n;
1505}
1506
1507node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001508PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001509 int start, int flags)
1510{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001511 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001512 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1513 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001514 if (n == NULL)
1515 err_input(&err);
1516 return n;
1517}
1518
1519node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001520PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001521{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001522 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001523}
1524
Guido van Rossum66ebd912003-04-17 16:02:26 +00001525/* May want to move a more generalized form of this to parsetok.c or
1526 even parser modules. */
1527
1528void
1529PyParser_SetError(perrdetail *err)
1530{
1531 err_input(err);
1532}
1533
Guido van Rossuma110aa61994-08-29 12:50:44 +00001534/* Set the error appropriate to the given input error code (see errcode.h) */
1535
1536static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001537err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001538{
Fred Drake85f36392000-07-11 17:53:00 +00001539 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001540 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001541 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001542 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001543 switch (err->error) {
1544 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001545 errtype = PyExc_IndentationError;
1546 if (err->expected == INDENT)
1547 msg = "expected an indented block";
1548 else if (err->token == INDENT)
1549 msg = "unexpected indent";
1550 else if (err->token == DEDENT)
1551 msg = "unexpected unindent";
1552 else {
1553 errtype = PyExc_SyntaxError;
1554 msg = "invalid syntax";
1555 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001556 break;
1557 case E_TOKEN:
1558 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001559 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001560 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001561 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001562 break;
1563 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001564 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001565 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001566 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001567 if (!PyErr_Occurred())
1568 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001569 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001570 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001571 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001572 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001573 case E_EOF:
1574 msg = "unexpected EOF while parsing";
1575 break;
Fred Drake85f36392000-07-11 17:53:00 +00001576 case E_TABSPACE:
1577 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001578 msg = "inconsistent use of tabs and spaces in indentation";
1579 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001580 case E_OVERFLOW:
1581 msg = "expression too long";
1582 break;
Fred Drake85f36392000-07-11 17:53:00 +00001583 case E_DEDENT:
1584 errtype = PyExc_IndentationError;
1585 msg = "unindent does not match any outer indentation level";
1586 break;
1587 case E_TOODEEP:
1588 errtype = PyExc_IndentationError;
1589 msg = "too many levels of indentation";
1590 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001591 case E_DECODE: {
1592 PyObject *type, *value, *tb;
1593 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001594 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001595 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001596 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001597 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001598 }
1599 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001600 if (msg == NULL)
1601 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001602 Py_XDECREF(type);
1603 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001604 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001605 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001606 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001607 case E_LINECONT:
1608 msg = "unexpected character after line continuation character";
1609 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001610 default:
1611 fprintf(stderr, "error=%d\n", err->error);
1612 msg = "unknown parsing error";
1613 break;
1614 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 v = Py_BuildValue("(ziiz)", err->filename,
1616 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 w = NULL;
1618 if (v != NULL)
1619 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001620 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001621 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001622 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001623 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001624cleanup:
1625 if (err->text != NULL) {
1626 PyObject_FREE(err->text);
1627 err->text = NULL;
1628 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001629}
1630
1631/* Print fatal error message and abort */
1632
1633void
Tim Peters7c321a82002-07-09 02:57:01 +00001634Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001635{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001636 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller42f9b4e2009-03-31 22:20:35 +00001637 fflush(stderr); /* it helps in Windows debug build */
1638
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001639#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +00001640 {
1641 size_t len = strlen(msg);
1642 WCHAR* buffer;
1643 size_t i;
1644
1645 /* Convert the message to wchar_t. This uses a simple one-to-one
1646 conversion, assuming that the this error message actually uses ASCII
1647 only. If this ceases to be true, we will have to convert. */
1648 buffer = alloca( (len+1) * (sizeof *buffer));
1649 for( i=0; i<=len; ++i)
1650 buffer[i] = msg[i];
1651 OutputDebugStringW(L"Fatal Python error: ");
1652 OutputDebugStringW(buffer);
1653 OutputDebugStringW(L"\n");
1654 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001655#ifdef _DEBUG
1656 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001657#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001658#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001659 abort();
1660}
1661
1662/* Clean up and exit */
1663
Guido van Rossuma110aa61994-08-29 12:50:44 +00001664#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001665#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001666#endif
1667
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001668#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001669static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001670static int nexitfuncs = 0;
1671
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001672int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001673{
1674 if (nexitfuncs >= NEXITFUNCS)
1675 return -1;
1676 exitfuncs[nexitfuncs++] = func;
1677 return 0;
1678}
1679
Guido van Rossumcc283f51997-08-05 02:22:03 +00001680static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001681call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001682{
Guido van Rossum82598051997-03-05 00:20:32 +00001683 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001684
1685 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001686 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001687 Py_INCREF(exitfunc);
1688 PySys_SetObject("exitfunc", (PyObject *)NULL);
1689 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001690 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001691 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1692 PySys_WriteStderr("Error in sys.exitfunc:\n");
1693 }
Guido van Rossum82598051997-03-05 00:20:32 +00001694 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001695 }
Guido van Rossum82598051997-03-05 00:20:32 +00001696 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001697 }
1698
Guido van Rossum0829c751998-02-28 04:31:39 +00001699 if (Py_FlushLine())
1700 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001701}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001702
Guido van Rossumcc283f51997-08-05 02:22:03 +00001703static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001704call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001705{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001706 while (nexitfuncs > 0)
1707 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001708
1709 fflush(stdout);
1710 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001711}
1712
1713void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001714Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001715{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001716 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001717
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001718 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001719}
1720
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001721static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001722initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001723{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001724#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001725 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001726#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001727#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001728 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001729#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001730#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001731 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001732#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001733 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001734}
1735
Guido van Rossum7433b121997-02-14 19:45:36 +00001736
1737/*
1738 * The file descriptor fd is considered ``interactive'' if either
1739 * a) isatty(fd) is TRUE, or
1740 * b) the -i flag was given, and the filename associated with
1741 * the descriptor is NULL or "<stdin>" or "???".
1742 */
1743int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001744Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001745{
1746 if (isatty((int)fileno(fp)))
1747 return 1;
1748 if (!Py_InteractiveFlag)
1749 return 0;
1750 return (filename == NULL) ||
1751 (strcmp(filename, "<stdin>") == 0) ||
1752 (strcmp(filename, "???") == 0);
1753}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001754
1755
Tim Petersd08e3822003-04-17 15:24:21 +00001756#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001757#if defined(WIN32) && defined(_MSC_VER)
1758
1759/* Stack checking for Microsoft C */
1760
1761#include <malloc.h>
1762#include <excpt.h>
1763
Fred Drakee8de31c2000-08-31 05:38:39 +00001764/*
1765 * Return non-zero when we run out of memory on the stack; zero otherwise.
1766 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001767int
Fred Drake399739f2000-08-31 05:52:44 +00001768PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001769{
1770 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001771 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001772 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001773 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001774 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001775 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1776 EXCEPTION_EXECUTE_HANDLER :
1777 EXCEPTION_CONTINUE_SEARCH) {
1778 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001779 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001780 {
1781 Py_FatalError("Could not reset the stack!");
1782 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001783 }
1784 return 1;
1785}
1786
1787#endif /* WIN32 && _MSC_VER */
1788
1789/* Alternate implementations can be added here... */
1790
1791#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001792
1793
1794/* Wrappers around sigaction() or signal(). */
1795
1796PyOS_sighandler_t
1797PyOS_getsig(int sig)
1798{
1799#ifdef HAVE_SIGACTION
1800 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001801 if (sigaction(sig, NULL, &context) == -1)
1802 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001803 return context.sa_handler;
1804#else
1805 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001806/* Special signal handling for the secure CRT in Visual Studio 2005 */
1807#if defined(_MSC_VER) && _MSC_VER >= 1400
1808 switch (sig) {
1809 /* Only these signals are valid */
1810 case SIGINT:
1811 case SIGILL:
1812 case SIGFPE:
1813 case SIGSEGV:
1814 case SIGTERM:
1815 case SIGBREAK:
1816 case SIGABRT:
1817 break;
1818 /* Don't call signal() with other values or it will assert */
1819 default:
1820 return SIG_ERR;
1821 }
1822#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001823 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001824 if (handler != SIG_ERR)
1825 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001826 return handler;
1827#endif
1828}
1829
1830PyOS_sighandler_t
1831PyOS_setsig(int sig, PyOS_sighandler_t handler)
1832{
1833#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001834 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001835 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001836 sigemptyset(&context.sa_mask);
1837 context.sa_flags = 0;
1838 if (sigaction(sig, &context, &ocontext) == -1)
1839 return SIG_ERR;
1840 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001841#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001842 PyOS_sighandler_t oldhandler;
1843 oldhandler = signal(sig, handler);
1844#ifdef HAVE_SIGINTERRUPT
1845 siginterrupt(sig, 1);
1846#endif
1847 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001848#endif
1849}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850
1851/* Deprecated C API functions still provided for binary compatiblity */
1852
1853#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001854PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1856{
1857 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1858}
1859
Thomas Heller1b046642006-04-18 18:51:06 +00001860#undef PyParser_SimpleParseString
1861PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862PyParser_SimpleParseString(const char *str, int start)
1863{
1864 return PyParser_SimpleParseStringFlags(str, start, 0);
1865}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001866
Thomas Heller1b046642006-04-18 18:51:06 +00001867#undef PyRun_AnyFile
1868PyAPI_FUNC(int)
1869PyRun_AnyFile(FILE *fp, const char *name)
1870{
1871 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1872}
1873
1874#undef PyRun_AnyFileEx
1875PyAPI_FUNC(int)
1876PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1877{
1878 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1879}
1880
1881#undef PyRun_AnyFileFlags
1882PyAPI_FUNC(int)
1883PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1884{
1885 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1886}
1887
1888#undef PyRun_File
1889PyAPI_FUNC(PyObject *)
1890PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1891{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001892 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001893}
1894
1895#undef PyRun_FileEx
1896PyAPI_FUNC(PyObject *)
1897PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1898{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001899 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001900}
1901
1902#undef PyRun_FileFlags
1903PyAPI_FUNC(PyObject *)
1904PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1905 PyCompilerFlags *flags)
1906{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001907 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001908}
1909
1910#undef PyRun_SimpleFile
1911PyAPI_FUNC(int)
1912PyRun_SimpleFile(FILE *f, const char *p)
1913{
1914 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1915}
1916
1917#undef PyRun_SimpleFileEx
1918PyAPI_FUNC(int)
1919PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1920{
1921 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1922}
1923
1924
1925#undef PyRun_String
1926PyAPI_FUNC(PyObject *)
1927PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1928{
1929 return PyRun_StringFlags(str, s, g, l, NULL);
1930}
1931
1932#undef PyRun_SimpleString
1933PyAPI_FUNC(int)
1934PyRun_SimpleString(const char *s)
1935{
1936 return PyRun_SimpleStringFlags(s, NULL);
1937}
1938
1939#undef Py_CompileString
1940PyAPI_FUNC(PyObject *)
1941Py_CompileString(const char *str, const char *p, int s)
1942{
1943 return Py_CompileStringFlags(str, p, s, NULL);
1944}
1945
1946#undef PyRun_InteractiveOne
1947PyAPI_FUNC(int)
1948PyRun_InteractiveOne(FILE *f, const char *p)
1949{
1950 return PyRun_InteractiveOneFlags(f, p, NULL);
1951}
1952
1953#undef PyRun_InteractiveLoop
1954PyAPI_FUNC(int)
1955PyRun_InteractiveLoop(FILE *f, const char *p)
1956{
1957 return PyRun_InteractiveLoopFlags(f, p, NULL);
1958}
1959
Anthony Baxterac6bd462006-04-13 02:06:09 +00001960#ifdef __cplusplus
1961}
1962#endif
1963