blob: f0dcfb382b1a3f1900c2e996230fa43ae0ada91b [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"
Antoine Pitrou9aece752009-10-27 12:48:52 +000020#include "abstract.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000022#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000024#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000025
Georg Brandl734373c2009-01-03 21:55:17 +000026#ifdef MS_WINDOWS
27#include "malloc.h" /* for alloca */
28#endif
29
Martin v. Löwis73d538b2003-03-05 15:13:47 +000030#ifdef HAVE_LANGINFO_H
31#include <locale.h>
32#include <langinfo.h>
33#endif
34
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000035#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#undef BYTE
37#include "windows.h"
38#endif
39
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000041#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000042#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000043#define PRINT_TOTAL_REFS() fprintf(stderr, \
44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000045 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000046#endif
47
Anthony Baxterac6bd462006-04-13 02:06:09 +000048#ifdef __cplusplus
49extern "C" {
50#endif
51
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossum82598051997-03-05 00:20:32 +000054extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055
Guido van Rossumb73cc041993-11-01 16:28:59 +000056/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initmain(void);
58static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000060 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000061static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000062 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static void err_input(perrdetail *);
64static void initsigs(void);
Antoine Pitrou9aece752009-10-27 12:48:52 +000065static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void call_sys_exitfunc(void);
67static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068extern void _PyUnicode_Init(void);
69extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000070
Mark Hammond8d98d2c2003-04-19 15:41:53 +000071#ifdef WITH_THREAD
72extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73extern void _PyGILState_Fini(void);
74#endif /* WITH_THREAD */
75
Guido van Rossum82598051997-03-05 00:20:32 +000076int Py_DebugFlag; /* Needed by parser.c */
77int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000078int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000079int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000080int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000081int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000082int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000083int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000084int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000085int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000086int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000087/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
88 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
89 true divisions (which they will be in 2.3). */
90int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000091int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000092
Brett Cannone9746892008-04-12 23:44:07 +000093/* PyModule_GetWarningsModule is no longer necessary as of 2.6
94since _warnings is builtin. This API should not be used. */
95PyObject *
96PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000097{
Brett Cannone9746892008-04-12 23:44:07 +000098 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000099}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000100
Guido van Rossum25ce5661997-08-02 03:10:38 +0000101static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000102
Thomas Wouters7e474022000-07-16 12:04:32 +0000103/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000104
105int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000106Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000107{
108 return initialized;
109}
110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111/* Global initializations. Can be undone by Py_Finalize(). Don't
112 call this twice without an intervening Py_Finalize() call. When
113 initializations fail, a fatal error is issued and the function does
114 not return. On return, the first thread and interpreter state have
115 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117 Locking: you must hold the interpreter lock while calling this.
118 (If the lock has not yet been initialized, that's equivalent to
119 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000120
Guido van Rossum25ce5661997-08-02 03:10:38 +0000121*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000122
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000123static int
124add_flag(int flag, const char *envs)
125{
126 int env = atoi(envs);
127 if (flag < env)
128 flag = env;
129 if (flag < 1)
130 flag = 1;
131 return flag;
132}
133
Guido van Rossuma027efa1997-05-05 20:56:21 +0000134void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000135Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000136{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000137 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000138 PyThreadState *tstate;
139 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000140 char *p;
Neal Norwitz18aa3882008-08-24 05:04:52 +0000141 char *icodeset = NULL; /* On Windows, input codeset may theoretically
142 differ from output codeset. */
Martin v. Löwis99815892008-06-01 07:20:46 +0000143 char *codeset = NULL;
144 char *errors = NULL;
145 int free_codeset = 0;
146 int overridden = 0;
Martin v. Löwisb12d8572008-06-01 08:06:17 +0000147 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000148#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis99815892008-06-01 07:20:46 +0000149 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000150#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000151#ifdef MS_WINDOWS
152 char ibuf[128];
153 char buf[128];
154#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000155 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000156
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000157 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000158 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000159 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000160
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000161 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000162 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000163 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000164 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000165 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000166 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000167 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
168 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000169
Guido van Rossuma027efa1997-05-05 20:56:21 +0000170 interp = PyInterpreterState_New();
171 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000172 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000173
Guido van Rossuma027efa1997-05-05 20:56:21 +0000174 tstate = PyThreadState_New(interp);
175 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000176 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000177 (void) PyThreadState_Swap(tstate);
178
Guido van Rossum70d893a2001-08-16 08:21:42 +0000179 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000180
Neal Norwitzb2501f42002-12-31 03:42:13 +0000181 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000182 Py_FatalError("Py_Initialize: can't init frames");
183
Neal Norwitzb2501f42002-12-31 03:42:13 +0000184 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000185 Py_FatalError("Py_Initialize: can't init ints");
186
Christian Heimes3497f942008-05-26 12:29:14 +0000187 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000188 Py_FatalError("Py_Initialize: can't init bytearray");
189
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000190 _PyFloat_Init();
191
Guido van Rossum25ce5661997-08-02 03:10:38 +0000192 interp->modules = PyDict_New();
193 if (interp->modules == NULL)
194 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000195 interp->modules_reloading = PyDict_New();
196 if (interp->modules_reloading == NULL)
197 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000198
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000199#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000200 /* Init Unicode implementation; relies on the codec registry */
201 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000202#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000203
Barry Warsawf242aa02000-05-25 23:09:49 +0000204 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205 if (bimod == NULL)
206 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000207 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000208 if (interp->builtins == NULL)
209 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000210 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000211
212 sysmod = _PySys_Init();
213 if (sysmod == NULL)
214 Py_FatalError("Py_Initialize: can't initialize sys");
215 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000216 if (interp->sysdict == NULL)
217 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000218 Py_INCREF(interp->sysdict);
219 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000220 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000221 PyDict_SetItemString(interp->sysdict, "modules",
222 interp->modules);
223
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000224 _PyImport_Init();
225
Barry Warsawf242aa02000-05-25 23:09:49 +0000226 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000227 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000228 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000229
Barry Warsaw035574d1997-08-29 22:07:17 +0000230 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000231 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000232
Just van Rossum52e14d62002-12-30 22:08:05 +0000233 _PyImportHooks_Init();
234
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000235 if (install_sigs)
236 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000237
Georg Brandl3c0fd562008-07-05 10:07:18 +0000238 /* Initialize warnings. */
239 _PyWarnings_Init();
240 if (PySys_HasWarnOptions()) {
241 PyObject *warnings_module = PyImport_ImportModule("warnings");
242 if (!warnings_module)
243 PyErr_Clear();
244 Py_XDECREF(warnings_module);
245 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000246
247 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000248 if (!Py_NoSiteFlag)
249 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000250
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000251 /* auto-thread-state API, if available */
252#ifdef WITH_THREAD
253 _PyGILState_Init(interp, tstate);
254#endif /* WITH_THREAD */
255
Martin v. Löwis99815892008-06-01 07:20:46 +0000256 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
257 p = icodeset = codeset = strdup(p);
258 free_codeset = 1;
259 errors = strchr(p, ':');
260 if (errors) {
261 *errors = '\0';
262 errors++;
263 }
264 overridden = 1;
265 }
266
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000267#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
268 /* On Unix, set the file system encoding according to the
269 user's preference, if the CODESET names a well-known
270 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000271 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000272 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000273
Martin v. Löwis99815892008-06-01 07:20:46 +0000274 if (!overridden || !Py_FileSystemDefaultEncoding) {
275 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
276 setlocale(LC_CTYPE, "");
277 loc_codeset = nl_langinfo(CODESET);
278 if (loc_codeset && *loc_codeset) {
279 PyObject *enc = PyCodec_Encoder(loc_codeset);
280 if (enc) {
281 loc_codeset = strdup(loc_codeset);
282 Py_DECREF(enc);
283 } else {
284 loc_codeset = NULL;
285 PyErr_Clear();
286 }
287 } else
288 loc_codeset = NULL;
289 setlocale(LC_CTYPE, saved_locale);
290 free(saved_locale);
291
292 if (!overridden) {
293 codeset = icodeset = loc_codeset;
294 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000295 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000296
297 /* Initialize Py_FileSystemDefaultEncoding from
298 locale even if PYTHONIOENCODING is set. */
299 if (!Py_FileSystemDefaultEncoding) {
300 Py_FileSystemDefaultEncoding = loc_codeset;
301 if (!overridden)
302 free_codeset = 0;
303 }
304 }
305#endif
306
307#ifdef MS_WINDOWS
308 if (!overridden) {
309 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000310 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000311 sprintf(ibuf, "cp%d", GetConsoleCP());
312 sprintf(buf, "cp%d", GetConsoleOutputCP());
313 }
314#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000315
316 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000317 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000318 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
319 if (!sys_isatty)
320 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000321 if ((overridden ||
322 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000323 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000324 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000325 Py_FatalError("Cannot set codeset of stdin");
326 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000327 Py_XDECREF(sys_isatty);
328
329 sys_stream = PySys_GetObject("stdout");
330 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
331 if (!sys_isatty)
332 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000333 if ((overridden ||
334 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000335 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000336 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000337 Py_FatalError("Cannot set codeset of stdout");
338 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000339 Py_XDECREF(sys_isatty);
340
Martin v. Löwisea62d252006-04-03 10:56:49 +0000341 sys_stream = PySys_GetObject("stderr");
342 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
343 if (!sys_isatty)
344 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000345 if((overridden ||
346 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000347 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000348 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000349 Py_FatalError("Cannot set codeset of stderr");
350 }
351 Py_XDECREF(sys_isatty);
352
Martin v. Löwis99815892008-06-01 07:20:46 +0000353 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000354 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000355 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000356}
357
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000358void
359Py_Initialize(void)
360{
361 Py_InitializeEx(1);
362}
363
364
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000365#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000366extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000367#endif
368
Guido van Rossum25ce5661997-08-02 03:10:38 +0000369/* Undo the effect of Py_Initialize().
370
371 Beware: if multiple interpreter and/or thread states exist, these
372 are not wiped out; only the current thread and interpreter state
373 are deleted. But since everything else is deleted, those other
374 interpreter and thread states should no longer be used.
375
376 (XXX We should do better, e.g. wipe out all interpreters and
377 threads.)
378
379 Locking: as above.
380
381*/
382
383void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000384Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385{
386 PyInterpreterState *interp;
387 PyThreadState *tstate;
388
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000389 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000390 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391
Antoine Pitrou9aece752009-10-27 12:48:52 +0000392 wait_for_thread_shutdown();
393
Tim Peters384fd102001-01-21 03:40:37 +0000394 /* The interpreter is still entirely intact at this point, and the
395 * exit funcs may be relying on that. In particular, if some thread
396 * or exit func is still waiting to do an import, the import machinery
397 * expects Py_IsInitialized() to return true. So don't say the
398 * interpreter is uninitialized until after the exit funcs have run.
399 * Note that Threading.py uses an exit func to do a join on all the
400 * threads created thru it, so this also protects pending imports in
401 * the threads created via Threading.
402 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000403 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000404 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000405
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000406 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000407 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000408 interp = tstate->interp;
409
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000410 /* Disable signal handling */
411 PyOS_FiniInterrupts();
412
Christian Heimes908caac2008-01-27 23:34:59 +0000413 /* Clear type lookup cache */
414 PyType_ClearCache();
415
Guido van Rossume13ddc92003-04-17 17:29:22 +0000416 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000417 * before all modules are destroyed.
418 * XXX If a __del__ or weakref callback is triggered here, and tries to
419 * XXX import a module, bad things can happen, because Python no
420 * XXX longer believes it's initialized.
421 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
422 * XXX is easy to provoke that way. I've also seen, e.g.,
423 * XXX Exception exceptions.ImportError: 'No module named sha'
424 * XXX in <function callback at 0x008F5718> ignored
425 * XXX but I'm unclear on exactly how that one happens. In any case,
426 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000427 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000428 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000429#ifdef COUNT_ALLOCS
430 /* With COUNT_ALLOCS, it helps to run GC multiple times:
431 each collection might release some types from the type
432 list, so they become garbage. */
433 while (PyGC_Collect() > 0)
434 /* nothing */;
435#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000436
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000437 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000438 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000439
Guido van Rossume13ddc92003-04-17 17:29:22 +0000440 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000441 * new-style class definitions, for example.
442 * XXX This is disabled because it caused too many problems. If
443 * XXX a __del__ or weakref callback triggers here, Python code has
444 * XXX a hard time running, because even the sys module has been
445 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
446 * XXX One symptom is a sequence of information-free messages
447 * XXX coming from threads (if a __del__ or callback is invoked,
448 * XXX other threads can execute too, and any exception they encounter
449 * XXX triggers a comedy of errors as subsystem after subsystem
450 * XXX fails to find what it *expects* to find in sys to help report
451 * XXX the exception and consequent unexpected failures). I've also
452 * XXX seen segfaults then, after adding print statements to the
453 * XXX Python code getting called.
454 */
455#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000456 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000457#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000458
Guido van Rossum1707aad1997-12-08 23:43:45 +0000459 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
460 _PyImport_Fini();
461
462 /* Debugging stuff */
463#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000464 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000465#endif
466
Tim Peters62e97f02006-03-28 21:44:32 +0000467 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000468
Tim Peters9cf25ce2003-04-17 15:21:01 +0000469#ifdef Py_TRACE_REFS
470 /* Display all objects still alive -- this can invoke arbitrary
471 * __repr__ overrides, so requires a mostly-intact interpreter.
472 * Alas, a lot of stuff may still be alive now that will be cleaned
473 * up later.
474 */
Tim Peters269b2a62003-04-17 19:52:29 +0000475 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000476 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000477#endif /* Py_TRACE_REFS */
478
Guido van Rossumd922fa42003-04-15 14:10:09 +0000479 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000480 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000481
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000482 /* Now we decref the exception classes. After this point nothing
483 can raise an exception. That's okay, because each Fini() method
484 below has been checked to make sure no exceptions are ever
485 raised.
486 */
487
488 _PyExc_Fini();
489
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000490 /* Cleanup auto-thread-state */
491#ifdef WITH_THREAD
492 _PyGILState_Fini();
493#endif /* WITH_THREAD */
494
Guido van Rossumd922fa42003-04-15 14:10:09 +0000495 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000496 PyThreadState_Swap(NULL);
497 PyInterpreterState_Delete(interp);
498
Guido van Rossumd922fa42003-04-15 14:10:09 +0000499 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000500 PyMethod_Fini();
501 PyFrame_Fini();
502 PyCFunction_Fini();
503 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000504 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000505 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000506 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000507 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000508 PyInt_Fini();
509 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000510 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000511
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000512#ifdef Py_USING_UNICODE
513 /* Cleanup Unicode implementation */
514 _PyUnicode_Fini();
515#endif
516
Guido van Rossumcc283f51997-08-05 02:22:03 +0000517 /* XXX Still allocated:
518 - various static ad-hoc pointers to interned strings
519 - int and float free list blocks
520 - whatever various modules and libraries allocate
521 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000522
523 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000524
Tim Peters269b2a62003-04-17 19:52:29 +0000525#ifdef Py_TRACE_REFS
526 /* Display addresses (& refcnts) of all objects still alive.
527 * An address can be used to find the repr of the object, printed
528 * above by _Py_PrintReferences.
529 */
530 if (Py_GETENV("PYTHONDUMPREFS"))
531 _Py_PrintReferenceAddresses(stderr);
532#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000533#ifdef PYMALLOC_DEBUG
534 if (Py_GETENV("PYTHONMALLOCSTATS"))
535 _PyObject_DebugMallocStats();
536#endif
537
Guido van Rossumcc283f51997-08-05 02:22:03 +0000538 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000539}
540
541/* Create and initialize a new interpreter and thread, and return the
542 new thread. This requires that Py_Initialize() has been called
543 first.
544
545 Unsuccessful initialization yields a NULL pointer. Note that *no*
546 exception information is available even in this case -- the
547 exception information is held in the thread, and there is no
548 thread.
549
550 Locking: as above.
551
552*/
553
554PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000555Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000556{
557 PyInterpreterState *interp;
558 PyThreadState *tstate, *save_tstate;
559 PyObject *bimod, *sysmod;
560
561 if (!initialized)
562 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
563
564 interp = PyInterpreterState_New();
565 if (interp == NULL)
566 return NULL;
567
568 tstate = PyThreadState_New(interp);
569 if (tstate == NULL) {
570 PyInterpreterState_Delete(interp);
571 return NULL;
572 }
573
574 save_tstate = PyThreadState_Swap(tstate);
575
576 /* XXX The following is lax in error checking */
577
578 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000579 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000580
581 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
582 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000583 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000584 if (interp->builtins == NULL)
585 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000586 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587 }
588 sysmod = _PyImport_FindExtension("sys", "sys");
589 if (bimod != NULL && sysmod != NULL) {
590 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000591 if (interp->sysdict == NULL)
592 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 Py_INCREF(interp->sysdict);
594 PySys_SetPath(Py_GetPath());
595 PyDict_SetItemString(interp->sysdict, "modules",
596 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000597 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000598 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000599 if (!Py_NoSiteFlag)
600 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 }
602
603 if (!PyErr_Occurred())
604 return tstate;
605
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000606handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 /* Oops, it didn't work. Undo it all. */
608
609 PyErr_Print();
610 PyThreadState_Clear(tstate);
611 PyThreadState_Swap(save_tstate);
612 PyThreadState_Delete(tstate);
613 PyInterpreterState_Delete(interp);
614
615 return NULL;
616}
617
618/* Delete an interpreter and its last thread. This requires that the
619 given thread state is current, that the thread has no remaining
620 frames, and that it is its interpreter's only remaining thread.
621 It is a fatal error to violate these constraints.
622
623 (Py_Finalize() doesn't have these constraints -- it zaps
624 everything, regardless.)
625
626 Locking: as above.
627
628*/
629
630void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000631Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000632{
633 PyInterpreterState *interp = tstate->interp;
634
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000635 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000636 Py_FatalError("Py_EndInterpreter: thread is not current");
637 if (tstate->frame != NULL)
638 Py_FatalError("Py_EndInterpreter: thread still has a frame");
639 if (tstate != interp->tstate_head || tstate->next != NULL)
640 Py_FatalError("Py_EndInterpreter: not the last thread");
641
642 PyImport_Cleanup();
643 PyInterpreterState_Clear(interp);
644 PyThreadState_Swap(NULL);
645 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000646}
647
648static char *progname = "python";
649
650void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000652{
653 if (pn && *pn)
654 progname = pn;
655}
656
657char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000658Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000659{
660 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000661}
662
Guido van Rossuma61691e1998-02-06 22:27:24 +0000663static char *default_home = NULL;
664
665void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000667{
668 default_home = home;
669}
670
671char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000672Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000673{
674 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000675 if (home == NULL && !Py_IgnoreEnvironmentFlag)
676 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000677 return home;
678}
679
Guido van Rossum6135a871995-01-09 17:53:26 +0000680/* Create __main__ module */
681
682static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000684{
Guido van Rossum82598051997-03-05 00:20:32 +0000685 PyObject *m, *d;
686 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000687 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000688 Py_FatalError("can't create __main__ module");
689 d = PyModule_GetDict(m);
690 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000691 PyObject *bimod = PyImport_ImportModule("__builtin__");
692 if (bimod == NULL ||
693 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000694 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000695 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000696 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000697}
698
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000699/* Import the site module (not into __main__ though) */
700
701static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000702initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000703{
704 PyObject *m, *f;
705 m = PyImport_ImportModule("site");
706 if (m == NULL) {
707 f = PySys_GetObject("stderr");
708 if (Py_VerboseFlag) {
709 PyFile_WriteString(
710 "'import site' failed; traceback:\n", f);
711 PyErr_Print();
712 }
713 else {
714 PyFile_WriteString(
715 "'import site' failed; use -v for traceback\n", f);
716 PyErr_Clear();
717 }
718 }
719 else {
720 Py_DECREF(m);
721 }
722}
723
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000724/* Parse input from a file and execute it */
725
726int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000727PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000728 PyCompilerFlags *flags)
729{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730 if (filename == NULL)
731 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000732 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000733 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000734 if (closeit)
735 fclose(fp);
736 return err;
737 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000738 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000739 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000740}
741
742int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000743PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000744{
Guido van Rossum82598051997-03-05 00:20:32 +0000745 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000746 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000747 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000748
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000749 if (flags == NULL) {
750 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000751 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000752 }
Guido van Rossum82598051997-03-05 00:20:32 +0000753 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000754 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000755 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000756 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000757 }
Guido van Rossum82598051997-03-05 00:20:32 +0000758 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000759 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000760 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000761 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000762 }
763 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000764 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000765 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000766 if (ret == E_EOF)
767 return 0;
768 /*
769 if (ret == E_NOMEM)
770 return -1;
771 */
772 }
773}
774
Eric Smith7c478942008-03-18 23:45:49 +0000775#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000776/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000777#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000778 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000779 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000780#endif
781#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000782/* Keep an example of flags with future keyword support. */
783#define PARSER_FLAGS(flags) \
784 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000785 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000786 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
787 PyPARSE_PRINT_IS_FUNCTION : 0) \
788 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
789 PyPARSE_UNICODE_LITERALS : 0) \
790 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000791#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000792
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000793int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000794PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000795{
Guido van Rossum82598051997-03-05 00:20:32 +0000796 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000798 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000799 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000801
Guido van Rossum82598051997-03-05 00:20:32 +0000802 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000803 if (v != NULL) {
804 v = PyObject_Str(v);
805 if (v == NULL)
806 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 else if (PyString_Check(v))
808 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000809 }
Guido van Rossum82598051997-03-05 00:20:32 +0000810 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000811 if (w != NULL) {
812 w = PyObject_Str(w);
813 if (w == NULL)
814 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000815 else if (PyString_Check(w))
816 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000817 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000818 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000819 if (arena == NULL) {
820 Py_XDECREF(v);
821 Py_XDECREF(w);
822 return -1;
823 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000824 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000827 Py_XDECREF(v);
828 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000830 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (errcode == E_EOF) {
832 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000833 return E_EOF;
834 }
Guido van Rossum82598051997-03-05 00:20:32 +0000835 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000837 }
Guido van Rossum82598051997-03-05 00:20:32 +0000838 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000839 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000840 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000841 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000842 }
Guido van Rossum82598051997-03-05 00:20:32 +0000843 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000844 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000845 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000846 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000847 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848 return -1;
849 }
Guido van Rossum82598051997-03-05 00:20:32 +0000850 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000851 if (Py_FlushLine())
852 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000853 return 0;
854}
855
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000856/* Check whether a file maybe a pyc file: Look at the extension,
857 the file type, and, if we may close it, at the first few bytes. */
858
859static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000860maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000861{
862 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
863 return 1;
864
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000865 /* Only look into the file if we are allowed to close it, since
866 it then should also be seekable. */
867 if (closeit) {
868 /* Read only two bytes of the magic. If the file was opened in
869 text mode, the bytes 3 and 4 of the magic (\r\n) might not
870 be read as they are on disk. */
871 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
872 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000873 /* Mess: In case of -x, the stream is NOT at its start now,
874 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000875 which makes the current stream position formally undefined,
876 and a x-platform nightmare.
877 Unfortunately, we have no direct way to know whether -x
878 was specified. So we use a terrible hack: if the current
879 stream position is not 0, we assume -x was specified, and
880 give up. Bug 132850 on SourceForge spells out the
881 hopelessness of trying anything else (fseek and ftell
882 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000883 */
Tim Peters3e876562001-02-11 04:35:39 +0000884 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000885 if (ftell(fp) == 0) {
886 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000887 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000888 ispyc = 1;
889 rewind(fp);
890 }
Tim Peters3e876562001-02-11 04:35:39 +0000891 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000892 }
893 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000894}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000895
Guido van Rossum0df002c2000-08-27 19:21:52 +0000896int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000897PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000898 PyCompilerFlags *flags)
899{
Guido van Rossum82598051997-03-05 00:20:32 +0000900 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000901 const char *ext;
Matthias Klosea8da9e02009-04-04 14:19:56 +0000902 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000903
Guido van Rossum82598051997-03-05 00:20:32 +0000904 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000905 if (m == NULL)
906 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000907 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000908 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000909 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000910 if (f == NULL)
911 return -1;
912 if (PyDict_SetItemString(d, "__file__", f) < 0) {
913 Py_DECREF(f);
914 return -1;
915 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000916 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000917 Py_DECREF(f);
918 }
Matthias Klosea8da9e02009-04-04 14:19:56 +0000919 len = strlen(filename);
920 ext = filename + len - (len > 4 ? 4 : 0);
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000921 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000922 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000923 if (closeit)
924 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000925 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000926 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000927 ret = -1;
928 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000929 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000930 /* Turn on optimization if a .pyo file is given */
931 if (strcmp(ext, ".pyo") == 0)
932 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000933 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000934 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000935 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000936 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000937 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000938 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000939 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000940 ret = -1;
941 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000942 }
Guido van Rossum82598051997-03-05 00:20:32 +0000943 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000944 if (Py_FlushLine())
945 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000946 ret = 0;
947 done:
948 if (set_file_name && PyDict_DelItemString(d, "__file__"))
949 PyErr_Clear();
950 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000951}
952
953int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000954PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000955{
Guido van Rossum82598051997-03-05 00:20:32 +0000956 PyObject *m, *d, *v;
957 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000958 if (m == NULL)
959 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000960 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000961 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000962 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000963 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964 return -1;
965 }
Guido van Rossum82598051997-03-05 00:20:32 +0000966 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000967 if (Py_FlushLine())
968 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000969 return 0;
970}
971
Barry Warsaw035574d1997-08-29 22:07:17 +0000972static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000973parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
974 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000975{
976 long hold;
977 PyObject *v;
978
979 /* old style errors */
980 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000981 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000982 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000983
984 /* new style errors. `err' is an instance */
985
986 if (! (v = PyObject_GetAttrString(err, "msg")))
987 goto finally;
988 *message = v;
989
990 if (!(v = PyObject_GetAttrString(err, "filename")))
991 goto finally;
992 if (v == Py_None)
993 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000994 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000995 goto finally;
996
997 Py_DECREF(v);
998 if (!(v = PyObject_GetAttrString(err, "lineno")))
999 goto finally;
1000 hold = PyInt_AsLong(v);
1001 Py_DECREF(v);
1002 v = NULL;
1003 if (hold < 0 && PyErr_Occurred())
1004 goto finally;
1005 *lineno = (int)hold;
1006
1007 if (!(v = PyObject_GetAttrString(err, "offset")))
1008 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001009 if (v == Py_None) {
1010 *offset = -1;
1011 Py_DECREF(v);
1012 v = NULL;
1013 } else {
1014 hold = PyInt_AsLong(v);
1015 Py_DECREF(v);
1016 v = NULL;
1017 if (hold < 0 && PyErr_Occurred())
1018 goto finally;
1019 *offset = (int)hold;
1020 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001021
1022 if (!(v = PyObject_GetAttrString(err, "text")))
1023 goto finally;
1024 if (v == Py_None)
1025 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001026 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001027 goto finally;
1028 Py_DECREF(v);
1029 return 1;
1030
1031finally:
1032 Py_XDECREF(v);
1033 return 0;
1034}
1035
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001036void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001037PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001038{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001039 PyErr_PrintEx(1);
1040}
1041
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001042static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001043print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001044{
1045 char *nl;
1046 if (offset >= 0) {
1047 if (offset > 0 && offset == (int)strlen(text))
1048 offset--;
1049 for (;;) {
1050 nl = strchr(text, '\n');
1051 if (nl == NULL || nl-text >= offset)
1052 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001053 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001054 text = nl+1;
1055 }
1056 while (*text == ' ' || *text == '\t') {
1057 text++;
1058 offset--;
1059 }
1060 }
1061 PyFile_WriteString(" ", f);
1062 PyFile_WriteString(text, f);
1063 if (*text == '\0' || text[strlen(text)-1] != '\n')
1064 PyFile_WriteString("\n", f);
1065 if (offset == -1)
1066 return;
1067 PyFile_WriteString(" ", f);
1068 offset--;
1069 while (offset > 0) {
1070 PyFile_WriteString(" ", f);
1071 offset--;
1072 }
1073 PyFile_WriteString("^\n", f);
1074}
1075
Guido van Rossum66e8e862001-03-23 17:54:43 +00001076static void
1077handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001078{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001079 PyObject *exception, *value, *tb;
1080 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001081
Georg Brandl49aafc92007-03-07 00:34:46 +00001082 if (Py_InspectFlag)
1083 /* Don't exit if -i flag was given. This flag is set to 0
1084 * when entering interactive mode for inspecting. */
1085 return;
1086
Guido van Rossum66e8e862001-03-23 17:54:43 +00001087 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001088 if (Py_FlushLine())
1089 PyErr_Clear();
1090 fflush(stdout);
1091 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001092 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001093 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001094 /* The error code should be in the `code' attribute. */
1095 PyObject *code = PyObject_GetAttrString(value, "code");
1096 if (code) {
1097 Py_DECREF(value);
1098 value = code;
1099 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001100 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001101 }
1102 /* If we failed to dig out the 'code' attribute,
1103 just let the else clause below print the error. */
1104 }
1105 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001106 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001107 else {
1108 PyObject_Print(value, stderr, Py_PRINT_RAW);
1109 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001110 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001111 }
Tim Peterscf615b52003-04-19 18:47:02 +00001112 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001113 /* Restore and clear the exception info, in order to properly decref
1114 * the exception, value, and traceback. If we just exit instead,
1115 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1116 * some finalizers from running.
1117 */
Tim Peterscf615b52003-04-19 18:47:02 +00001118 PyErr_Restore(exception, value, tb);
1119 PyErr_Clear();
1120 Py_Exit(exitcode);
1121 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001122}
1123
1124void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001125PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001126{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001127 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001128
1129 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1130 handle_system_exit();
1131 }
Guido van Rossum82598051997-03-05 00:20:32 +00001132 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 if (exception == NULL)
1134 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001135 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001136 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001137 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001138 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001139 if (set_sys_last_vars) {
1140 PySys_SetObject("last_type", exception);
1141 PySys_SetObject("last_value", v);
1142 PySys_SetObject("last_traceback", tb);
1143 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001144 hook = PySys_GetObject("excepthook");
1145 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001146 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001147 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001148 PyObject *result = PyEval_CallObject(hook, args);
1149 if (result == NULL) {
1150 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001151 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1152 handle_system_exit();
1153 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001154 PyErr_Fetch(&exception2, &v2, &tb2);
1155 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001156 /* It should not be possible for exception2 or v2
1157 to be NULL. However PyErr_Display() can't
1158 tolerate NULLs, so just be safe. */
1159 if (exception2 == NULL) {
1160 exception2 = Py_None;
1161 Py_INCREF(exception2);
1162 }
1163 if (v2 == NULL) {
1164 v2 = Py_None;
1165 Py_INCREF(v2);
1166 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001167 if (Py_FlushLine())
1168 PyErr_Clear();
1169 fflush(stdout);
1170 PySys_WriteStderr("Error in sys.excepthook:\n");
1171 PyErr_Display(exception2, v2, tb2);
1172 PySys_WriteStderr("\nOriginal exception was:\n");
1173 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001174 Py_DECREF(exception2);
1175 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001176 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001177 }
1178 Py_XDECREF(result);
1179 Py_XDECREF(args);
1180 } else {
1181 PySys_WriteStderr("sys.excepthook is missing\n");
1182 PyErr_Display(exception, v, tb);
1183 }
1184 Py_XDECREF(exception);
1185 Py_XDECREF(v);
1186 Py_XDECREF(tb);
1187}
1188
Richard Jones7b9558d2006-05-27 12:29:24 +00001189void
1190PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001191{
1192 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001193 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001194 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001195 if (f == NULL)
1196 fprintf(stderr, "lost sys.stderr\n");
1197 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001198 if (Py_FlushLine())
1199 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001200 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001201 if (tb && tb != Py_None)
1202 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001203 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001204 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001205 {
Guido van Rossum82598051997-03-05 00:20:32 +00001206 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001207 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001208 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001209 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001210 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001211 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001212 else {
1213 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001214 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001215 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001216 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001217 else
Guido van Rossum82598051997-03-05 00:20:32 +00001218 PyFile_WriteString(filename, f);
1219 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001220 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001221 PyFile_WriteString(buf, f);
1222 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001223 if (text != NULL)
1224 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001225 Py_DECREF(value);
1226 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001227 /* Can't be bothered to check all those
1228 PyFile_WriteString() calls */
1229 if (PyErr_Occurred())
1230 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001231 }
1232 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001233 if (err) {
1234 /* Don't do anything else */
1235 }
Brett Cannonbf364092006-03-01 04:25:17 +00001236 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001237 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001238 char* className = PyExceptionClass_Name(exception);
1239 if (className != NULL) {
1240 char *dot = strrchr(className, '.');
1241 if (dot != NULL)
1242 className = dot+1;
1243 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001244
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001245 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001246 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001247 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001248 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001249 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001250 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001251 {
1252 err = PyFile_WriteString(modstr, f);
1253 err += PyFile_WriteString(".", f);
1254 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001255 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001256 }
1257 if (err == 0) {
1258 if (className == NULL)
1259 err = PyFile_WriteString("<unknown>", f);
1260 else
Brett Cannonbf364092006-03-01 04:25:17 +00001261 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001262 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001263 }
1264 else
1265 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001266 if (err == 0 && (value != Py_None)) {
1267 PyObject *s = PyObject_Str(value);
1268 /* only print colon if the str() of the
1269 object is not the empty string
1270 */
1271 if (s == NULL)
1272 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001273 else if (!PyString_Check(s) ||
1274 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001275 err = PyFile_WriteString(": ", f);
1276 if (err == 0)
1277 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1278 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001279 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001280 /* try to write a newline in any case */
1281 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001282 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001283 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001284 /* If an error happened here, don't show it.
1285 XXX This is wrong, but too many callers rely on this behavior. */
1286 if (err != 0)
1287 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288}
1289
Guido van Rossum82598051997-03-05 00:20:32 +00001290PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001291PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001293{
Neal Norwitze92fba02006-03-04 18:52:26 +00001294 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001295 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001296 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001297 if (arena == NULL)
1298 return NULL;
1299
1300 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001301 if (mod != NULL)
1302 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001303 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001305}
1306
1307PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001308PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001309 PyObject *locals, int closeit, PyCompilerFlags *flags)
1310{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001312 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001313 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001314 if (arena == NULL)
1315 return NULL;
1316
1317 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1318 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001319 if (closeit)
1320 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001321 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001322 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001324 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001325 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001326 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001328}
1329
Guido van Rossum82598051997-03-05 00:20:32 +00001330static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001332 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001333{
Guido van Rossum82598051997-03-05 00:20:32 +00001334 PyCodeObject *co;
1335 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001336 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001337 if (co == NULL)
1338 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001339 v = PyEval_EvalCode(co, globals, locals);
1340 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001341 return v;
1342}
1343
Guido van Rossum82598051997-03-05 00:20:32 +00001344static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001345run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001347{
Guido van Rossum82598051997-03-05 00:20:32 +00001348 PyCodeObject *co;
1349 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001350 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001351 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001352
Guido van Rossum82598051997-03-05 00:20:32 +00001353 magic = PyMarshal_ReadLongFromFile(fp);
1354 if (magic != PyImport_GetMagicNumber()) {
1355 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001356 "Bad magic number in .pyc file");
1357 return NULL;
1358 }
Guido van Rossum82598051997-03-05 00:20:32 +00001359 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001360 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001361 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001362 if (v == NULL || !PyCode_Check(v)) {
1363 Py_XDECREF(v);
1364 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001365 "Bad code object in .pyc file");
1366 return NULL;
1367 }
Guido van Rossum82598051997-03-05 00:20:32 +00001368 co = (PyCodeObject *)v;
1369 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001370 if (v && flags)
1371 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001372 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001373 return v;
1374}
1375
Guido van Rossum82598051997-03-05 00:20:32 +00001376PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001377Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001378 PyCompilerFlags *flags)
1379{
Guido van Rossum82598051997-03-05 00:20:32 +00001380 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001381 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001382 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001383 if (arena == NULL)
1384 return NULL;
1385
1386 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001387 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001388 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001389 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001390 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001391 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001392 PyObject *result = PyAST_mod2obj(mod);
1393 PyArena_Free(arena);
1394 return result;
1395 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001396 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001397 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001398 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001399}
1400
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001401struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001402Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001403{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001404 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001405 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001406 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001407 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001408 if (arena == NULL)
1409 return NULL;
1410
Christian Heimes7f23d862008-03-26 22:51:58 +00001411 flags.cf_flags = 0;
1412
Christian Heimes3c608332008-03-26 22:01:37 +00001413 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001415 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001416 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001419 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001420 return st;
1421}
1422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423/* Preferred access to parser is through AST. */
1424mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001425PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001426 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001429 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001431 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001432
1433 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001434 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001435 &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001436 if (flags == NULL) {
1437 localflags.cf_flags = 0;
1438 flags = &localflags;
1439 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001441 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 PyNode_Free(n);
1444 return mod;
1445 }
1446 else {
1447 err_input(&err);
1448 return NULL;
1449 }
1450}
1451
1452mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001453PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001454 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001455 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001458 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001460 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001461
Christian Heimes3c608332008-03-26 22:01:37 +00001462 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1463 start, ps1, ps2, &err, &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001464 if (flags == NULL) {
1465 localflags.cf_flags = 0;
1466 flags = &localflags;
1467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001469 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001470 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 PyNode_Free(n);
1472 return mod;
1473 }
1474 else {
1475 err_input(&err);
1476 if (errcode)
1477 *errcode = err.error;
1478 return NULL;
1479 }
1480}
1481
Guido van Rossuma110aa61994-08-29 12:50:44 +00001482/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001483
Guido van Rossuma110aa61994-08-29 12:50:44 +00001484node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001485PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001486{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001487 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1489 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001490 if (n == NULL)
1491 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001492
Guido van Rossuma110aa61994-08-29 12:50:44 +00001493 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001494}
1495
Guido van Rossuma110aa61994-08-29 12:50:44 +00001496/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001497
Guido van Rossuma110aa61994-08-29 12:50:44 +00001498node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001499PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001500{
Tim Petersfe2127d2001-07-16 05:37:24 +00001501 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001502 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1503 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001504 if (n == NULL)
1505 err_input(&err);
1506 return n;
1507}
1508
1509node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001510PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001511 int start, int flags)
1512{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001513 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001514 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1515 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001516 if (n == NULL)
1517 err_input(&err);
1518 return n;
1519}
1520
1521node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001522PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001523{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001524 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001525}
1526
Guido van Rossum66ebd912003-04-17 16:02:26 +00001527/* May want to move a more generalized form of this to parsetok.c or
1528 even parser modules. */
1529
1530void
1531PyParser_SetError(perrdetail *err)
1532{
1533 err_input(err);
1534}
1535
Guido van Rossuma110aa61994-08-29 12:50:44 +00001536/* Set the error appropriate to the given input error code (see errcode.h) */
1537
1538static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001539err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001540{
Fred Drake85f36392000-07-11 17:53:00 +00001541 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001542 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001543 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001544 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001545 switch (err->error) {
1546 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001547 errtype = PyExc_IndentationError;
1548 if (err->expected == INDENT)
1549 msg = "expected an indented block";
1550 else if (err->token == INDENT)
1551 msg = "unexpected indent";
1552 else if (err->token == DEDENT)
1553 msg = "unexpected unindent";
1554 else {
1555 errtype = PyExc_SyntaxError;
1556 msg = "invalid syntax";
1557 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001558 break;
1559 case E_TOKEN:
1560 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001561 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001562 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001563 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001564 break;
1565 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001566 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001567 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001568 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001569 if (!PyErr_Occurred())
1570 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001571 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001572 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001573 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001574 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001575 case E_EOF:
1576 msg = "unexpected EOF while parsing";
1577 break;
Fred Drake85f36392000-07-11 17:53:00 +00001578 case E_TABSPACE:
1579 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001580 msg = "inconsistent use of tabs and spaces in indentation";
1581 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001582 case E_OVERFLOW:
1583 msg = "expression too long";
1584 break;
Fred Drake85f36392000-07-11 17:53:00 +00001585 case E_DEDENT:
1586 errtype = PyExc_IndentationError;
1587 msg = "unindent does not match any outer indentation level";
1588 break;
1589 case E_TOODEEP:
1590 errtype = PyExc_IndentationError;
1591 msg = "too many levels of indentation";
1592 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001593 case E_DECODE: {
1594 PyObject *type, *value, *tb;
1595 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001596 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001597 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001598 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001599 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001600 }
1601 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001602 if (msg == NULL)
1603 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001604 Py_XDECREF(type);
1605 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001606 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001607 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001608 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001609 case E_LINECONT:
1610 msg = "unexpected character after line continuation character";
1611 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001612 default:
1613 fprintf(stderr, "error=%d\n", err->error);
1614 msg = "unknown parsing error";
1615 break;
1616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 v = Py_BuildValue("(ziiz)", err->filename,
1618 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619 w = NULL;
1620 if (v != NULL)
1621 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001622 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001623 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001624 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001625 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001626cleanup:
1627 if (err->text != NULL) {
1628 PyObject_FREE(err->text);
1629 err->text = NULL;
1630 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001631}
1632
1633/* Print fatal error message and abort */
1634
1635void
Tim Peters7c321a82002-07-09 02:57:01 +00001636Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001637{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001638 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller70b11f02009-03-31 22:25:20 +00001639 fflush(stderr); /* it helps in Windows debug build */
1640
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001641#ifdef MS_WINDOWS
Georg Brandl734373c2009-01-03 21:55:17 +00001642 {
1643 size_t len = strlen(msg);
1644 WCHAR* buffer;
1645 size_t i;
1646
1647 /* Convert the message to wchar_t. This uses a simple one-to-one
1648 conversion, assuming that the this error message actually uses ASCII
1649 only. If this ceases to be true, we will have to convert. */
1650 buffer = alloca( (len+1) * (sizeof *buffer));
1651 for( i=0; i<=len; ++i)
1652 buffer[i] = msg[i];
1653 OutputDebugStringW(L"Fatal Python error: ");
1654 OutputDebugStringW(buffer);
1655 OutputDebugStringW(L"\n");
1656 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001657#ifdef _DEBUG
1658 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001659#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001660#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001661 abort();
1662}
1663
1664/* Clean up and exit */
1665
Guido van Rossuma110aa61994-08-29 12:50:44 +00001666#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001667#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001668#endif
1669
Antoine Pitrou9aece752009-10-27 12:48:52 +00001670/* Wait until threading._shutdown completes, provided
1671 the threading module was imported in the first place.
1672 The shutdown routine will wait until all non-daemon
1673 "threading" threads have completed. */
1674static void
1675wait_for_thread_shutdown(void)
1676{
1677#ifdef WITH_THREAD
1678 PyObject *result;
1679 PyThreadState *tstate = PyThreadState_GET();
1680 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1681 "threading");
1682 if (threading == NULL) {
1683 /* threading not imported */
1684 PyErr_Clear();
1685 return;
1686 }
1687 result = PyObject_CallMethod(threading, "_shutdown", "");
1688 if (result == NULL)
1689 PyErr_WriteUnraisable(threading);
1690 else
1691 Py_DECREF(result);
1692 Py_DECREF(threading);
1693#endif
1694}
1695
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001696#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001697static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001698static int nexitfuncs = 0;
1699
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001700int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001701{
1702 if (nexitfuncs >= NEXITFUNCS)
1703 return -1;
1704 exitfuncs[nexitfuncs++] = func;
1705 return 0;
1706}
1707
Guido van Rossumcc283f51997-08-05 02:22:03 +00001708static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001709call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001710{
Guido van Rossum82598051997-03-05 00:20:32 +00001711 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001712
1713 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001714 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001715 Py_INCREF(exitfunc);
1716 PySys_SetObject("exitfunc", (PyObject *)NULL);
1717 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001718 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001719 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1720 PySys_WriteStderr("Error in sys.exitfunc:\n");
1721 }
Guido van Rossum82598051997-03-05 00:20:32 +00001722 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001723 }
Guido van Rossum82598051997-03-05 00:20:32 +00001724 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001725 }
1726
Guido van Rossum0829c751998-02-28 04:31:39 +00001727 if (Py_FlushLine())
1728 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001729}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001730
Guido van Rossumcc283f51997-08-05 02:22:03 +00001731static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001732call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001733{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001734 while (nexitfuncs > 0)
1735 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001736
1737 fflush(stdout);
1738 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001739}
1740
1741void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001742Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001743{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001744 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001745
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001746 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001747}
1748
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001749static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001750initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001751{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001752#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001753 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001754#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001755#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001756 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001757#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001758#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001759 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001760#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001761 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001762}
1763
Guido van Rossum7433b121997-02-14 19:45:36 +00001764
1765/*
1766 * The file descriptor fd is considered ``interactive'' if either
1767 * a) isatty(fd) is TRUE, or
1768 * b) the -i flag was given, and the filename associated with
1769 * the descriptor is NULL or "<stdin>" or "???".
1770 */
1771int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001772Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001773{
1774 if (isatty((int)fileno(fp)))
1775 return 1;
1776 if (!Py_InteractiveFlag)
1777 return 0;
1778 return (filename == NULL) ||
1779 (strcmp(filename, "<stdin>") == 0) ||
1780 (strcmp(filename, "???") == 0);
1781}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001782
1783
Tim Petersd08e3822003-04-17 15:24:21 +00001784#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001785#if defined(WIN32) && defined(_MSC_VER)
1786
1787/* Stack checking for Microsoft C */
1788
1789#include <malloc.h>
1790#include <excpt.h>
1791
Fred Drakee8de31c2000-08-31 05:38:39 +00001792/*
1793 * Return non-zero when we run out of memory on the stack; zero otherwise.
1794 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001795int
Fred Drake399739f2000-08-31 05:52:44 +00001796PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001797{
1798 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001799 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001800 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001801 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001802 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001803 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1804 EXCEPTION_EXECUTE_HANDLER :
1805 EXCEPTION_CONTINUE_SEARCH) {
1806 int errcode = _resetstkoflw();
Amaury Forgeot d'Arc74b458c2008-11-22 20:06:51 +00001807 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001808 {
1809 Py_FatalError("Could not reset the stack!");
1810 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001811 }
1812 return 1;
1813}
1814
1815#endif /* WIN32 && _MSC_VER */
1816
1817/* Alternate implementations can be added here... */
1818
1819#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001820
1821
1822/* Wrappers around sigaction() or signal(). */
1823
1824PyOS_sighandler_t
1825PyOS_getsig(int sig)
1826{
1827#ifdef HAVE_SIGACTION
1828 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001829 if (sigaction(sig, NULL, &context) == -1)
1830 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001831 return context.sa_handler;
1832#else
1833 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001834/* Special signal handling for the secure CRT in Visual Studio 2005 */
1835#if defined(_MSC_VER) && _MSC_VER >= 1400
1836 switch (sig) {
1837 /* Only these signals are valid */
1838 case SIGINT:
1839 case SIGILL:
1840 case SIGFPE:
1841 case SIGSEGV:
1842 case SIGTERM:
1843 case SIGBREAK:
1844 case SIGABRT:
1845 break;
1846 /* Don't call signal() with other values or it will assert */
1847 default:
1848 return SIG_ERR;
1849 }
1850#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001851 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001852 if (handler != SIG_ERR)
1853 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001854 return handler;
1855#endif
1856}
1857
1858PyOS_sighandler_t
1859PyOS_setsig(int sig, PyOS_sighandler_t handler)
1860{
1861#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001862 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001863 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001864 sigemptyset(&context.sa_mask);
1865 context.sa_flags = 0;
1866 if (sigaction(sig, &context, &ocontext) == -1)
1867 return SIG_ERR;
1868 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001869#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001870 PyOS_sighandler_t oldhandler;
1871 oldhandler = signal(sig, handler);
1872#ifdef HAVE_SIGINTERRUPT
1873 siginterrupt(sig, 1);
1874#endif
1875 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001876#endif
1877}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878
1879/* Deprecated C API functions still provided for binary compatiblity */
1880
1881#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001882PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1884{
1885 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1886}
1887
Thomas Heller1b046642006-04-18 18:51:06 +00001888#undef PyParser_SimpleParseString
1889PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890PyParser_SimpleParseString(const char *str, int start)
1891{
1892 return PyParser_SimpleParseStringFlags(str, start, 0);
1893}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001894
Thomas Heller1b046642006-04-18 18:51:06 +00001895#undef PyRun_AnyFile
1896PyAPI_FUNC(int)
1897PyRun_AnyFile(FILE *fp, const char *name)
1898{
1899 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1900}
1901
1902#undef PyRun_AnyFileEx
1903PyAPI_FUNC(int)
1904PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1905{
1906 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1907}
1908
1909#undef PyRun_AnyFileFlags
1910PyAPI_FUNC(int)
1911PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1912{
1913 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1914}
1915
1916#undef PyRun_File
1917PyAPI_FUNC(PyObject *)
1918PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1919{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001920 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001921}
1922
1923#undef PyRun_FileEx
1924PyAPI_FUNC(PyObject *)
1925PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1926{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001927 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001928}
1929
1930#undef PyRun_FileFlags
1931PyAPI_FUNC(PyObject *)
1932PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1933 PyCompilerFlags *flags)
1934{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001935 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001936}
1937
1938#undef PyRun_SimpleFile
1939PyAPI_FUNC(int)
1940PyRun_SimpleFile(FILE *f, const char *p)
1941{
1942 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1943}
1944
1945#undef PyRun_SimpleFileEx
1946PyAPI_FUNC(int)
1947PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1948{
1949 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1950}
1951
1952
1953#undef PyRun_String
1954PyAPI_FUNC(PyObject *)
1955PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1956{
1957 return PyRun_StringFlags(str, s, g, l, NULL);
1958}
1959
1960#undef PyRun_SimpleString
1961PyAPI_FUNC(int)
1962PyRun_SimpleString(const char *s)
1963{
1964 return PyRun_SimpleStringFlags(s, NULL);
1965}
1966
1967#undef Py_CompileString
1968PyAPI_FUNC(PyObject *)
1969Py_CompileString(const char *str, const char *p, int s)
1970{
1971 return Py_CompileStringFlags(str, p, s, NULL);
1972}
1973
1974#undef PyRun_InteractiveOne
1975PyAPI_FUNC(int)
1976PyRun_InteractiveOne(FILE *f, const char *p)
1977{
1978 return PyRun_InteractiveOneFlags(f, p, NULL);
1979}
1980
1981#undef PyRun_InteractiveLoop
1982PyAPI_FUNC(int)
1983PyRun_InteractiveLoop(FILE *f, const char *p)
1984{
1985 return PyRun_InteractiveLoopFlags(f, p, NULL);
1986}
1987
Anthony Baxterac6bd462006-04-13 02:06:09 +00001988#ifdef __cplusplus
1989}
1990#endif
1991