blob: 8fc0ca199f2af2e31ce10b0a8d21a31347521c3f [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;
Matthias Kloseedb5e1e2009-04-04 14:18:13 +0000901 int set_file_name = 0, ret, len;
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 }
Matthias Kloseedb5e1e2009-04-04 14:18:13 +0000918 len = strlen(filename);
919 ext = filename + len - (len > 4 ? 4 : 0);
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000920 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000921 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000922 if (closeit)
923 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000924 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000925 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000926 ret = -1;
927 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000928 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000929 /* Turn on optimization if a .pyo file is given */
930 if (strcmp(ext, ".pyo") == 0)
931 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000932 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000933 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000934 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000935 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000936 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000937 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000938 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000939 ret = -1;
940 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000941 }
Guido van Rossum82598051997-03-05 00:20:32 +0000942 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000943 if (Py_FlushLine())
944 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000945 ret = 0;
946 done:
947 if (set_file_name && PyDict_DelItemString(d, "__file__"))
948 PyErr_Clear();
949 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000950}
951
952int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000953PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000954{
Guido van Rossum82598051997-03-05 00:20:32 +0000955 PyObject *m, *d, *v;
956 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957 if (m == NULL)
958 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000959 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000960 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000961 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000962 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000963 return -1;
964 }
Guido van Rossum82598051997-03-05 00:20:32 +0000965 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000966 if (Py_FlushLine())
967 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000968 return 0;
969}
970
Barry Warsaw035574d1997-08-29 22:07:17 +0000971static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000972parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
973 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000974{
975 long hold;
976 PyObject *v;
977
978 /* old style errors */
979 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000980 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000981 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000982
983 /* new style errors. `err' is an instance */
984
985 if (! (v = PyObject_GetAttrString(err, "msg")))
986 goto finally;
987 *message = v;
988
989 if (!(v = PyObject_GetAttrString(err, "filename")))
990 goto finally;
991 if (v == Py_None)
992 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000993 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000994 goto finally;
995
996 Py_DECREF(v);
997 if (!(v = PyObject_GetAttrString(err, "lineno")))
998 goto finally;
999 hold = PyInt_AsLong(v);
1000 Py_DECREF(v);
1001 v = NULL;
1002 if (hold < 0 && PyErr_Occurred())
1003 goto finally;
1004 *lineno = (int)hold;
1005
1006 if (!(v = PyObject_GetAttrString(err, "offset")))
1007 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001008 if (v == Py_None) {
1009 *offset = -1;
1010 Py_DECREF(v);
1011 v = NULL;
1012 } else {
1013 hold = PyInt_AsLong(v);
1014 Py_DECREF(v);
1015 v = NULL;
1016 if (hold < 0 && PyErr_Occurred())
1017 goto finally;
1018 *offset = (int)hold;
1019 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001020
1021 if (!(v = PyObject_GetAttrString(err, "text")))
1022 goto finally;
1023 if (v == Py_None)
1024 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001025 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001026 goto finally;
1027 Py_DECREF(v);
1028 return 1;
1029
1030finally:
1031 Py_XDECREF(v);
1032 return 0;
1033}
1034
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001035void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001036PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001037{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001038 PyErr_PrintEx(1);
1039}
1040
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001041static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001042print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001043{
1044 char *nl;
1045 if (offset >= 0) {
1046 if (offset > 0 && offset == (int)strlen(text))
1047 offset--;
1048 for (;;) {
1049 nl = strchr(text, '\n');
1050 if (nl == NULL || nl-text >= offset)
1051 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001052 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001053 text = nl+1;
1054 }
1055 while (*text == ' ' || *text == '\t') {
1056 text++;
1057 offset--;
1058 }
1059 }
1060 PyFile_WriteString(" ", f);
1061 PyFile_WriteString(text, f);
1062 if (*text == '\0' || text[strlen(text)-1] != '\n')
1063 PyFile_WriteString("\n", f);
1064 if (offset == -1)
1065 return;
1066 PyFile_WriteString(" ", f);
1067 offset--;
1068 while (offset > 0) {
1069 PyFile_WriteString(" ", f);
1070 offset--;
1071 }
1072 PyFile_WriteString("^\n", f);
1073}
1074
Guido van Rossum66e8e862001-03-23 17:54:43 +00001075static void
1076handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001077{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001078 PyObject *exception, *value, *tb;
1079 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001080
Georg Brandl49aafc92007-03-07 00:34:46 +00001081 if (Py_InspectFlag)
1082 /* Don't exit if -i flag was given. This flag is set to 0
1083 * when entering interactive mode for inspecting. */
1084 return;
1085
Guido van Rossum66e8e862001-03-23 17:54:43 +00001086 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001087 if (Py_FlushLine())
1088 PyErr_Clear();
1089 fflush(stdout);
1090 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001091 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001092 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001093 /* The error code should be in the `code' attribute. */
1094 PyObject *code = PyObject_GetAttrString(value, "code");
1095 if (code) {
1096 Py_DECREF(value);
1097 value = code;
1098 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001099 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001100 }
1101 /* If we failed to dig out the 'code' attribute,
1102 just let the else clause below print the error. */
1103 }
1104 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001105 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001106 else {
1107 PyObject_Print(value, stderr, Py_PRINT_RAW);
1108 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001109 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001110 }
Tim Peterscf615b52003-04-19 18:47:02 +00001111 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001112 /* Restore and clear the exception info, in order to properly decref
1113 * the exception, value, and traceback. If we just exit instead,
1114 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1115 * some finalizers from running.
1116 */
Tim Peterscf615b52003-04-19 18:47:02 +00001117 PyErr_Restore(exception, value, tb);
1118 PyErr_Clear();
1119 Py_Exit(exitcode);
1120 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001121}
1122
1123void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001124PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001125{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001126 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001127
1128 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1129 handle_system_exit();
1130 }
Guido van Rossum82598051997-03-05 00:20:32 +00001131 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 if (exception == NULL)
1133 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001134 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001135 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001136 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001137 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001138 if (set_sys_last_vars) {
1139 PySys_SetObject("last_type", exception);
1140 PySys_SetObject("last_value", v);
1141 PySys_SetObject("last_traceback", tb);
1142 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001143 hook = PySys_GetObject("excepthook");
1144 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001145 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001146 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001147 PyObject *result = PyEval_CallObject(hook, args);
1148 if (result == NULL) {
1149 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001150 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1151 handle_system_exit();
1152 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001153 PyErr_Fetch(&exception2, &v2, &tb2);
1154 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001155 /* It should not be possible for exception2 or v2
1156 to be NULL. However PyErr_Display() can't
1157 tolerate NULLs, so just be safe. */
1158 if (exception2 == NULL) {
1159 exception2 = Py_None;
1160 Py_INCREF(exception2);
1161 }
1162 if (v2 == NULL) {
1163 v2 = Py_None;
1164 Py_INCREF(v2);
1165 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001166 if (Py_FlushLine())
1167 PyErr_Clear();
1168 fflush(stdout);
1169 PySys_WriteStderr("Error in sys.excepthook:\n");
1170 PyErr_Display(exception2, v2, tb2);
1171 PySys_WriteStderr("\nOriginal exception was:\n");
1172 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001173 Py_DECREF(exception2);
1174 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001175 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001176 }
1177 Py_XDECREF(result);
1178 Py_XDECREF(args);
1179 } else {
1180 PySys_WriteStderr("sys.excepthook is missing\n");
1181 PyErr_Display(exception, v, tb);
1182 }
1183 Py_XDECREF(exception);
1184 Py_XDECREF(v);
1185 Py_XDECREF(tb);
1186}
1187
Richard Jones7b9558d2006-05-27 12:29:24 +00001188void
1189PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001190{
1191 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001192 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001193 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001194 if (f == NULL)
1195 fprintf(stderr, "lost sys.stderr\n");
1196 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001197 if (Py_FlushLine())
1198 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001199 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001200 if (tb && tb != Py_None)
1201 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001202 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001203 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001204 {
Guido van Rossum82598051997-03-05 00:20:32 +00001205 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001206 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001207 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001208 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001209 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001210 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001211 else {
1212 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001213 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001214 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001215 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001216 else
Guido van Rossum82598051997-03-05 00:20:32 +00001217 PyFile_WriteString(filename, f);
1218 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001219 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001220 PyFile_WriteString(buf, f);
1221 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001222 if (text != NULL)
1223 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001224 Py_DECREF(value);
1225 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001226 /* Can't be bothered to check all those
1227 PyFile_WriteString() calls */
1228 if (PyErr_Occurred())
1229 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001230 }
1231 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001232 if (err) {
1233 /* Don't do anything else */
1234 }
Brett Cannonbf364092006-03-01 04:25:17 +00001235 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001236 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001237 char* className = PyExceptionClass_Name(exception);
1238 if (className != NULL) {
1239 char *dot = strrchr(className, '.');
1240 if (dot != NULL)
1241 className = dot+1;
1242 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001243
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001244 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001245 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001246 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001247 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001248 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001249 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001250 {
1251 err = PyFile_WriteString(modstr, f);
1252 err += PyFile_WriteString(".", f);
1253 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001254 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001255 }
1256 if (err == 0) {
1257 if (className == NULL)
1258 err = PyFile_WriteString("<unknown>", f);
1259 else
Brett Cannonbf364092006-03-01 04:25:17 +00001260 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001261 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001262 }
1263 else
1264 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001265 if (err == 0 && (value != Py_None)) {
1266 PyObject *s = PyObject_Str(value);
1267 /* only print colon if the str() of the
1268 object is not the empty string
1269 */
1270 if (s == NULL)
1271 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001272 else if (!PyString_Check(s) ||
1273 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001274 err = PyFile_WriteString(": ", f);
1275 if (err == 0)
1276 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1277 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001278 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001279 /* try to write a newline in any case */
1280 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001281 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001282 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001283 /* If an error happened here, don't show it.
1284 XXX This is wrong, but too many callers rely on this behavior. */
1285 if (err != 0)
1286 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001287}
1288
Guido van Rossum82598051997-03-05 00:20:32 +00001289PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001290PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001292{
Neal Norwitze92fba02006-03-04 18:52:26 +00001293 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001294 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001295 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001296 if (arena == NULL)
1297 return NULL;
1298
1299 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001300 if (mod != NULL)
1301 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001302 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001304}
1305
1306PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001307PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001308 PyObject *locals, int closeit, PyCompilerFlags *flags)
1309{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001311 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001312 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001313 if (arena == NULL)
1314 return NULL;
1315
1316 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1317 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001318 if (closeit)
1319 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001320 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001321 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001323 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001324 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001325 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001327}
1328
Guido van Rossum82598051997-03-05 00:20:32 +00001329static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001331 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001332{
Guido van Rossum82598051997-03-05 00:20:32 +00001333 PyCodeObject *co;
1334 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001335 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001336 if (co == NULL)
1337 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001338 v = PyEval_EvalCode(co, globals, locals);
1339 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001340 return v;
1341}
1342
Guido van Rossum82598051997-03-05 00:20:32 +00001343static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001344run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001346{
Guido van Rossum82598051997-03-05 00:20:32 +00001347 PyCodeObject *co;
1348 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001349 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001350 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001351
Guido van Rossum82598051997-03-05 00:20:32 +00001352 magic = PyMarshal_ReadLongFromFile(fp);
1353 if (magic != PyImport_GetMagicNumber()) {
1354 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001355 "Bad magic number in .pyc file");
1356 return NULL;
1357 }
Guido van Rossum82598051997-03-05 00:20:32 +00001358 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001359 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001360 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001361 if (v == NULL || !PyCode_Check(v)) {
1362 Py_XDECREF(v);
1363 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001364 "Bad code object in .pyc file");
1365 return NULL;
1366 }
Guido van Rossum82598051997-03-05 00:20:32 +00001367 co = (PyCodeObject *)v;
1368 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001369 if (v && flags)
1370 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001371 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001372 return v;
1373}
1374
Guido van Rossum82598051997-03-05 00:20:32 +00001375PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001376Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001377 PyCompilerFlags *flags)
1378{
Guido van Rossum82598051997-03-05 00:20:32 +00001379 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001380 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001381 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001382 if (arena == NULL)
1383 return NULL;
1384
1385 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001386 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001387 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001388 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001389 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001390 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001391 PyObject *result = PyAST_mod2obj(mod);
1392 PyArena_Free(arena);
1393 return result;
1394 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001395 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001396 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001397 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001398}
1399
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001400struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001401Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001402{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001403 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001404 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001405 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001406 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001407 if (arena == NULL)
1408 return NULL;
1409
Christian Heimes7f23d862008-03-26 22:51:58 +00001410 flags.cf_flags = 0;
1411
Christian Heimes3c608332008-03-26 22:01:37 +00001412 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001413 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001414 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001415 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001418 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001419 return st;
1420}
1421
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422/* Preferred access to parser is through AST. */
1423mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001424PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001425 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001428 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001430 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001431
1432 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001433 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001434 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001435 if (flags == NULL) {
1436 localflags.cf_flags = 0;
1437 flags = &localflags;
1438 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001440 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001441 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 PyNode_Free(n);
1443 return mod;
1444 }
1445 else {
1446 err_input(&err);
1447 return NULL;
1448 }
1449}
1450
1451mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001452PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001453 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001454 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001457 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001459 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001460
Christian Heimes3c608332008-03-26 22:01:37 +00001461 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1462 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001463 if (flags == NULL) {
1464 localflags.cf_flags = 0;
1465 flags = &localflags;
1466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001468 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 PyNode_Free(n);
1471 return mod;
1472 }
1473 else {
1474 err_input(&err);
1475 if (errcode)
1476 *errcode = err.error;
1477 return NULL;
1478 }
1479}
1480
Guido van Rossuma110aa61994-08-29 12:50:44 +00001481/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001482
Guido van Rossuma110aa61994-08-29 12:50:44 +00001483node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001484PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001485{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001486 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001487 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1488 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001489 if (n == NULL)
1490 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001491
Guido van Rossuma110aa61994-08-29 12:50:44 +00001492 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001493}
1494
Guido van Rossuma110aa61994-08-29 12:50:44 +00001495/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001496
Guido van Rossuma110aa61994-08-29 12:50:44 +00001497node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001498PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001499{
Tim Petersfe2127d2001-07-16 05:37:24 +00001500 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001501 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1502 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001503 if (n == NULL)
1504 err_input(&err);
1505 return n;
1506}
1507
1508node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001509PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001510 int start, int flags)
1511{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001512 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001513 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1514 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001515 if (n == NULL)
1516 err_input(&err);
1517 return n;
1518}
1519
1520node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001521PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001522{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001523 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001524}
1525
Guido van Rossum66ebd912003-04-17 16:02:26 +00001526/* May want to move a more generalized form of this to parsetok.c or
1527 even parser modules. */
1528
1529void
1530PyParser_SetError(perrdetail *err)
1531{
1532 err_input(err);
1533}
1534
Guido van Rossuma110aa61994-08-29 12:50:44 +00001535/* Set the error appropriate to the given input error code (see errcode.h) */
1536
1537static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001538err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001539{
Fred Drake85f36392000-07-11 17:53:00 +00001540 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001541 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001542 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001543 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001544 switch (err->error) {
1545 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001546 errtype = PyExc_IndentationError;
1547 if (err->expected == INDENT)
1548 msg = "expected an indented block";
1549 else if (err->token == INDENT)
1550 msg = "unexpected indent";
1551 else if (err->token == DEDENT)
1552 msg = "unexpected unindent";
1553 else {
1554 errtype = PyExc_SyntaxError;
1555 msg = "invalid syntax";
1556 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001557 break;
1558 case E_TOKEN:
1559 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001560 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001561 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001562 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001563 break;
1564 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001565 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001566 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001567 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001568 if (!PyErr_Occurred())
1569 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001570 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001571 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001572 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001573 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001574 case E_EOF:
1575 msg = "unexpected EOF while parsing";
1576 break;
Fred Drake85f36392000-07-11 17:53:00 +00001577 case E_TABSPACE:
1578 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001579 msg = "inconsistent use of tabs and spaces in indentation";
1580 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001581 case E_OVERFLOW:
1582 msg = "expression too long";
1583 break;
Fred Drake85f36392000-07-11 17:53:00 +00001584 case E_DEDENT:
1585 errtype = PyExc_IndentationError;
1586 msg = "unindent does not match any outer indentation level";
1587 break;
1588 case E_TOODEEP:
1589 errtype = PyExc_IndentationError;
1590 msg = "too many levels of indentation";
1591 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001592 case E_DECODE: {
1593 PyObject *type, *value, *tb;
1594 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001595 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001596 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001597 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001598 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001599 }
1600 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001601 if (msg == NULL)
1602 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001603 Py_XDECREF(type);
1604 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001605 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001606 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001607 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001608 case E_LINECONT:
1609 msg = "unexpected character after line continuation character";
1610 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001611 default:
1612 fprintf(stderr, "error=%d\n", err->error);
1613 msg = "unknown parsing error";
1614 break;
1615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 v = Py_BuildValue("(ziiz)", err->filename,
1617 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 w = NULL;
1619 if (v != NULL)
1620 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001621 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001622 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001623 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001624 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001625cleanup:
1626 if (err->text != NULL) {
1627 PyObject_FREE(err->text);
1628 err->text = NULL;
1629 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001630}
1631
1632/* Print fatal error message and abort */
1633
1634void
Tim Peters7c321a82002-07-09 02:57:01 +00001635Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001636{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001637 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller42f9b4e2009-03-31 22:20:35 +00001638 fflush(stderr); /* it helps in Windows debug build */
1639
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001640#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +00001641 {
1642 size_t len = strlen(msg);
1643 WCHAR* buffer;
1644 size_t i;
1645
1646 /* Convert the message to wchar_t. This uses a simple one-to-one
1647 conversion, assuming that the this error message actually uses ASCII
1648 only. If this ceases to be true, we will have to convert. */
1649 buffer = alloca( (len+1) * (sizeof *buffer));
1650 for( i=0; i<=len; ++i)
1651 buffer[i] = msg[i];
1652 OutputDebugStringW(L"Fatal Python error: ");
1653 OutputDebugStringW(buffer);
1654 OutputDebugStringW(L"\n");
1655 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001656#ifdef _DEBUG
1657 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001658#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001659#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001660 abort();
1661}
1662
1663/* Clean up and exit */
1664
Guido van Rossuma110aa61994-08-29 12:50:44 +00001665#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001666#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001667#endif
1668
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001669#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001670static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001671static int nexitfuncs = 0;
1672
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001673int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001674{
1675 if (nexitfuncs >= NEXITFUNCS)
1676 return -1;
1677 exitfuncs[nexitfuncs++] = func;
1678 return 0;
1679}
1680
Guido van Rossumcc283f51997-08-05 02:22:03 +00001681static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001682call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001683{
Guido van Rossum82598051997-03-05 00:20:32 +00001684 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001685
1686 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001687 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001688 Py_INCREF(exitfunc);
1689 PySys_SetObject("exitfunc", (PyObject *)NULL);
1690 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001691 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001692 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1693 PySys_WriteStderr("Error in sys.exitfunc:\n");
1694 }
Guido van Rossum82598051997-03-05 00:20:32 +00001695 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001696 }
Guido van Rossum82598051997-03-05 00:20:32 +00001697 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001698 }
1699
Guido van Rossum0829c751998-02-28 04:31:39 +00001700 if (Py_FlushLine())
1701 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001702}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001703
Guido van Rossumcc283f51997-08-05 02:22:03 +00001704static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001705call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001706{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001707 while (nexitfuncs > 0)
1708 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001709
1710 fflush(stdout);
1711 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001712}
1713
1714void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001715Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001716{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001717 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001718
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001719 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001720}
1721
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001722static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001723initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001724{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001725#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001726 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001727#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001728#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001729 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001730#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001731#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001732 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001733#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001734 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001735}
1736
Guido van Rossum7433b121997-02-14 19:45:36 +00001737
1738/*
1739 * The file descriptor fd is considered ``interactive'' if either
1740 * a) isatty(fd) is TRUE, or
1741 * b) the -i flag was given, and the filename associated with
1742 * the descriptor is NULL or "<stdin>" or "???".
1743 */
1744int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001745Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001746{
1747 if (isatty((int)fileno(fp)))
1748 return 1;
1749 if (!Py_InteractiveFlag)
1750 return 0;
1751 return (filename == NULL) ||
1752 (strcmp(filename, "<stdin>") == 0) ||
1753 (strcmp(filename, "???") == 0);
1754}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001755
1756
Tim Petersd08e3822003-04-17 15:24:21 +00001757#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001758#if defined(WIN32) && defined(_MSC_VER)
1759
1760/* Stack checking for Microsoft C */
1761
1762#include <malloc.h>
1763#include <excpt.h>
1764
Fred Drakee8de31c2000-08-31 05:38:39 +00001765/*
1766 * Return non-zero when we run out of memory on the stack; zero otherwise.
1767 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001768int
Fred Drake399739f2000-08-31 05:52:44 +00001769PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001770{
1771 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001772 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001773 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001774 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001775 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001776 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1777 EXCEPTION_EXECUTE_HANDLER :
1778 EXCEPTION_CONTINUE_SEARCH) {
1779 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001780 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001781 {
1782 Py_FatalError("Could not reset the stack!");
1783 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001784 }
1785 return 1;
1786}
1787
1788#endif /* WIN32 && _MSC_VER */
1789
1790/* Alternate implementations can be added here... */
1791
1792#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001793
1794
1795/* Wrappers around sigaction() or signal(). */
1796
1797PyOS_sighandler_t
1798PyOS_getsig(int sig)
1799{
1800#ifdef HAVE_SIGACTION
1801 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001802 if (sigaction(sig, NULL, &context) == -1)
1803 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001804 return context.sa_handler;
1805#else
1806 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001807/* Special signal handling for the secure CRT in Visual Studio 2005 */
1808#if defined(_MSC_VER) && _MSC_VER >= 1400
1809 switch (sig) {
1810 /* Only these signals are valid */
1811 case SIGINT:
1812 case SIGILL:
1813 case SIGFPE:
1814 case SIGSEGV:
1815 case SIGTERM:
1816 case SIGBREAK:
1817 case SIGABRT:
1818 break;
1819 /* Don't call signal() with other values or it will assert */
1820 default:
1821 return SIG_ERR;
1822 }
1823#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001824 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001825 if (handler != SIG_ERR)
1826 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001827 return handler;
1828#endif
1829}
1830
1831PyOS_sighandler_t
1832PyOS_setsig(int sig, PyOS_sighandler_t handler)
1833{
1834#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001835 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001836 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001837 sigemptyset(&context.sa_mask);
1838 context.sa_flags = 0;
1839 if (sigaction(sig, &context, &ocontext) == -1)
1840 return SIG_ERR;
1841 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001842#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001843 PyOS_sighandler_t oldhandler;
1844 oldhandler = signal(sig, handler);
1845#ifdef HAVE_SIGINTERRUPT
1846 siginterrupt(sig, 1);
1847#endif
1848 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001849#endif
1850}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851
1852/* Deprecated C API functions still provided for binary compatiblity */
1853
1854#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001855PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1857{
1858 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1859}
1860
Thomas Heller1b046642006-04-18 18:51:06 +00001861#undef PyParser_SimpleParseString
1862PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863PyParser_SimpleParseString(const char *str, int start)
1864{
1865 return PyParser_SimpleParseStringFlags(str, start, 0);
1866}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001867
Thomas Heller1b046642006-04-18 18:51:06 +00001868#undef PyRun_AnyFile
1869PyAPI_FUNC(int)
1870PyRun_AnyFile(FILE *fp, const char *name)
1871{
1872 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1873}
1874
1875#undef PyRun_AnyFileEx
1876PyAPI_FUNC(int)
1877PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1878{
1879 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1880}
1881
1882#undef PyRun_AnyFileFlags
1883PyAPI_FUNC(int)
1884PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1885{
1886 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1887}
1888
1889#undef PyRun_File
1890PyAPI_FUNC(PyObject *)
1891PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1892{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001893 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001894}
1895
1896#undef PyRun_FileEx
1897PyAPI_FUNC(PyObject *)
1898PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1899{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001900 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001901}
1902
1903#undef PyRun_FileFlags
1904PyAPI_FUNC(PyObject *)
1905PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1906 PyCompilerFlags *flags)
1907{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001908 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001909}
1910
1911#undef PyRun_SimpleFile
1912PyAPI_FUNC(int)
1913PyRun_SimpleFile(FILE *f, const char *p)
1914{
1915 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1916}
1917
1918#undef PyRun_SimpleFileEx
1919PyAPI_FUNC(int)
1920PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1921{
1922 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1923}
1924
1925
1926#undef PyRun_String
1927PyAPI_FUNC(PyObject *)
1928PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1929{
1930 return PyRun_StringFlags(str, s, g, l, NULL);
1931}
1932
1933#undef PyRun_SimpleString
1934PyAPI_FUNC(int)
1935PyRun_SimpleString(const char *s)
1936{
1937 return PyRun_SimpleStringFlags(s, NULL);
1938}
1939
1940#undef Py_CompileString
1941PyAPI_FUNC(PyObject *)
1942Py_CompileString(const char *str, const char *p, int s)
1943{
1944 return Py_CompileStringFlags(str, p, s, NULL);
1945}
1946
1947#undef PyRun_InteractiveOne
1948PyAPI_FUNC(int)
1949PyRun_InteractiveOne(FILE *f, const char *p)
1950{
1951 return PyRun_InteractiveOneFlags(f, p, NULL);
1952}
1953
1954#undef PyRun_InteractiveLoop
1955PyAPI_FUNC(int)
1956PyRun_InteractiveLoop(FILE *f, const char *p)
1957{
1958 return PyRun_InteractiveLoopFlags(f, p, NULL);
1959}
1960
Anthony Baxterac6bd462006-04-13 02:06:09 +00001961#ifdef __cplusplus
1962}
1963#endif
1964