blob: 67740432291eed8806652a93f15acdebf8bd44f7 [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__ */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000248
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000249 /* auto-thread-state API, if available */
250#ifdef WITH_THREAD
251 _PyGILState_Init(interp, tstate);
252#endif /* WITH_THREAD */
253
Victor Stinnerea164292010-03-21 14:02:32 +0000254 if (!Py_NoSiteFlag)
255 initsite(); /* Module site */
256
Martin v. Löwis99815892008-06-01 07:20:46 +0000257 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
258 p = icodeset = codeset = strdup(p);
259 free_codeset = 1;
260 errors = strchr(p, ':');
261 if (errors) {
262 *errors = '\0';
263 errors++;
264 }
265 overridden = 1;
266 }
267
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000268#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
269 /* On Unix, set the file system encoding according to the
270 user's preference, if the CODESET names a well-known
271 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000272 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000273 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000274
Martin v. Löwis99815892008-06-01 07:20:46 +0000275 if (!overridden || !Py_FileSystemDefaultEncoding) {
276 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
277 setlocale(LC_CTYPE, "");
278 loc_codeset = nl_langinfo(CODESET);
279 if (loc_codeset && *loc_codeset) {
280 PyObject *enc = PyCodec_Encoder(loc_codeset);
281 if (enc) {
282 loc_codeset = strdup(loc_codeset);
283 Py_DECREF(enc);
284 } else {
Victor Stinnerea164292010-03-21 14:02:32 +0000285 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
286 PyErr_Clear();
287 loc_codeset = NULL;
288 } else {
289 PyErr_Print();
290 exit(1);
291 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000292 }
293 } else
294 loc_codeset = NULL;
295 setlocale(LC_CTYPE, saved_locale);
296 free(saved_locale);
297
298 if (!overridden) {
299 codeset = icodeset = loc_codeset;
300 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000301 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000302
303 /* Initialize Py_FileSystemDefaultEncoding from
304 locale even if PYTHONIOENCODING is set. */
305 if (!Py_FileSystemDefaultEncoding) {
306 Py_FileSystemDefaultEncoding = loc_codeset;
307 if (!overridden)
308 free_codeset = 0;
309 }
310 }
311#endif
312
313#ifdef MS_WINDOWS
314 if (!overridden) {
315 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000316 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000317 sprintf(ibuf, "cp%d", GetConsoleCP());
318 sprintf(buf, "cp%d", GetConsoleOutputCP());
319 }
320#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000321
322 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000323 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000324 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
325 if (!sys_isatty)
326 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000327 if ((overridden ||
328 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000329 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000330 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000331 Py_FatalError("Cannot set codeset of stdin");
332 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000333 Py_XDECREF(sys_isatty);
334
335 sys_stream = PySys_GetObject("stdout");
336 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
337 if (!sys_isatty)
338 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000339 if ((overridden ||
340 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000341 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000342 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000343 Py_FatalError("Cannot set codeset of stdout");
344 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000345 Py_XDECREF(sys_isatty);
346
Martin v. Löwisea62d252006-04-03 10:56:49 +0000347 sys_stream = PySys_GetObject("stderr");
348 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
349 if (!sys_isatty)
350 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000351 if((overridden ||
352 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000353 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000354 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000355 Py_FatalError("Cannot set codeset of stderr");
356 }
357 Py_XDECREF(sys_isatty);
358
Martin v. Löwis99815892008-06-01 07:20:46 +0000359 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000360 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000361 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000362}
363
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000364void
365Py_Initialize(void)
366{
367 Py_InitializeEx(1);
368}
369
370
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000371#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000372extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000373#endif
374
Guido van Rossum25ce5661997-08-02 03:10:38 +0000375/* Undo the effect of Py_Initialize().
376
377 Beware: if multiple interpreter and/or thread states exist, these
378 are not wiped out; only the current thread and interpreter state
379 are deleted. But since everything else is deleted, those other
380 interpreter and thread states should no longer be used.
381
382 (XXX We should do better, e.g. wipe out all interpreters and
383 threads.)
384
385 Locking: as above.
386
387*/
388
389void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000390Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000391{
392 PyInterpreterState *interp;
393 PyThreadState *tstate;
394
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000395 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000396 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000397
Antoine Pitrou9aece752009-10-27 12:48:52 +0000398 wait_for_thread_shutdown();
399
Tim Peters384fd102001-01-21 03:40:37 +0000400 /* The interpreter is still entirely intact at this point, and the
401 * exit funcs may be relying on that. In particular, if some thread
402 * or exit func is still waiting to do an import, the import machinery
403 * expects Py_IsInitialized() to return true. So don't say the
404 * interpreter is uninitialized until after the exit funcs have run.
405 * Note that Threading.py uses an exit func to do a join on all the
406 * threads created thru it, so this also protects pending imports in
407 * the threads created via Threading.
408 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000409 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000410 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000411
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000412 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000413 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414 interp = tstate->interp;
415
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000416 /* Disable signal handling */
417 PyOS_FiniInterrupts();
418
Christian Heimes908caac2008-01-27 23:34:59 +0000419 /* Clear type lookup cache */
420 PyType_ClearCache();
421
Guido van Rossume13ddc92003-04-17 17:29:22 +0000422 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000423 * before all modules are destroyed.
424 * XXX If a __del__ or weakref callback is triggered here, and tries to
425 * XXX import a module, bad things can happen, because Python no
426 * XXX longer believes it's initialized.
427 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
428 * XXX is easy to provoke that way. I've also seen, e.g.,
429 * XXX Exception exceptions.ImportError: 'No module named sha'
430 * XXX in <function callback at 0x008F5718> ignored
431 * XXX but I'm unclear on exactly how that one happens. In any case,
432 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000433 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000434 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000435#ifdef COUNT_ALLOCS
436 /* With COUNT_ALLOCS, it helps to run GC multiple times:
437 each collection might release some types from the type
438 list, so they become garbage. */
439 while (PyGC_Collect() > 0)
440 /* nothing */;
441#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000442
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000443 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000444 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000445
Guido van Rossume13ddc92003-04-17 17:29:22 +0000446 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000447 * new-style class definitions, for example.
448 * XXX This is disabled because it caused too many problems. If
449 * XXX a __del__ or weakref callback triggers here, Python code has
450 * XXX a hard time running, because even the sys module has been
451 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
452 * XXX One symptom is a sequence of information-free messages
453 * XXX coming from threads (if a __del__ or callback is invoked,
454 * XXX other threads can execute too, and any exception they encounter
455 * XXX triggers a comedy of errors as subsystem after subsystem
456 * XXX fails to find what it *expects* to find in sys to help report
457 * XXX the exception and consequent unexpected failures). I've also
458 * XXX seen segfaults then, after adding print statements to the
459 * XXX Python code getting called.
460 */
461#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000462 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000463#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000464
Guido van Rossum1707aad1997-12-08 23:43:45 +0000465 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
466 _PyImport_Fini();
467
468 /* Debugging stuff */
469#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000470 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000471#endif
472
Tim Peters62e97f02006-03-28 21:44:32 +0000473 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000474
Tim Peters9cf25ce2003-04-17 15:21:01 +0000475#ifdef Py_TRACE_REFS
476 /* Display all objects still alive -- this can invoke arbitrary
477 * __repr__ overrides, so requires a mostly-intact interpreter.
478 * Alas, a lot of stuff may still be alive now that will be cleaned
479 * up later.
480 */
Tim Peters269b2a62003-04-17 19:52:29 +0000481 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000482 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000483#endif /* Py_TRACE_REFS */
484
Guido van Rossumd922fa42003-04-15 14:10:09 +0000485 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000486 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000487
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000488 /* Now we decref the exception classes. After this point nothing
489 can raise an exception. That's okay, because each Fini() method
490 below has been checked to make sure no exceptions are ever
491 raised.
492 */
493
494 _PyExc_Fini();
495
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000496 /* Cleanup auto-thread-state */
497#ifdef WITH_THREAD
498 _PyGILState_Fini();
499#endif /* WITH_THREAD */
500
Guido van Rossumd922fa42003-04-15 14:10:09 +0000501 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000502 PyThreadState_Swap(NULL);
503 PyInterpreterState_Delete(interp);
504
Guido van Rossumd922fa42003-04-15 14:10:09 +0000505 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000506 PyMethod_Fini();
507 PyFrame_Fini();
508 PyCFunction_Fini();
509 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000510 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000511 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000512 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000513 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000514 PyInt_Fini();
515 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000516 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000517
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000518#ifdef Py_USING_UNICODE
519 /* Cleanup Unicode implementation */
520 _PyUnicode_Fini();
521#endif
522
Guido van Rossumcc283f51997-08-05 02:22:03 +0000523 /* XXX Still allocated:
524 - various static ad-hoc pointers to interned strings
525 - int and float free list blocks
526 - whatever various modules and libraries allocate
527 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000528
529 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000530
Tim Peters269b2a62003-04-17 19:52:29 +0000531#ifdef Py_TRACE_REFS
532 /* Display addresses (& refcnts) of all objects still alive.
533 * An address can be used to find the repr of the object, printed
534 * above by _Py_PrintReferences.
535 */
536 if (Py_GETENV("PYTHONDUMPREFS"))
537 _Py_PrintReferenceAddresses(stderr);
538#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000539#ifdef PYMALLOC_DEBUG
540 if (Py_GETENV("PYTHONMALLOCSTATS"))
541 _PyObject_DebugMallocStats();
542#endif
543
Guido van Rossumcc283f51997-08-05 02:22:03 +0000544 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000545}
546
547/* Create and initialize a new interpreter and thread, and return the
548 new thread. This requires that Py_Initialize() has been called
549 first.
550
551 Unsuccessful initialization yields a NULL pointer. Note that *no*
552 exception information is available even in this case -- the
553 exception information is held in the thread, and there is no
554 thread.
555
556 Locking: as above.
557
558*/
559
560PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000562{
563 PyInterpreterState *interp;
564 PyThreadState *tstate, *save_tstate;
565 PyObject *bimod, *sysmod;
566
567 if (!initialized)
568 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
569
570 interp = PyInterpreterState_New();
571 if (interp == NULL)
572 return NULL;
573
574 tstate = PyThreadState_New(interp);
575 if (tstate == NULL) {
576 PyInterpreterState_Delete(interp);
577 return NULL;
578 }
579
580 save_tstate = PyThreadState_Swap(tstate);
581
582 /* XXX The following is lax in error checking */
583
584 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000585 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000586
587 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
588 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000589 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000590 if (interp->builtins == NULL)
591 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000592 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 }
594 sysmod = _PyImport_FindExtension("sys", "sys");
595 if (bimod != NULL && sysmod != NULL) {
596 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000597 if (interp->sysdict == NULL)
598 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599 Py_INCREF(interp->sysdict);
600 PySys_SetPath(Py_GetPath());
601 PyDict_SetItemString(interp->sysdict, "modules",
602 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000603 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000604 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000605 if (!Py_NoSiteFlag)
606 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000607 }
608
609 if (!PyErr_Occurred())
610 return tstate;
611
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000612handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000613 /* Oops, it didn't work. Undo it all. */
614
615 PyErr_Print();
616 PyThreadState_Clear(tstate);
617 PyThreadState_Swap(save_tstate);
618 PyThreadState_Delete(tstate);
619 PyInterpreterState_Delete(interp);
620
621 return NULL;
622}
623
624/* Delete an interpreter and its last thread. This requires that the
625 given thread state is current, that the thread has no remaining
626 frames, and that it is its interpreter's only remaining thread.
627 It is a fatal error to violate these constraints.
628
629 (Py_Finalize() doesn't have these constraints -- it zaps
630 everything, regardless.)
631
632 Locking: as above.
633
634*/
635
636void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000637Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000638{
639 PyInterpreterState *interp = tstate->interp;
640
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000641 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000642 Py_FatalError("Py_EndInterpreter: thread is not current");
643 if (tstate->frame != NULL)
644 Py_FatalError("Py_EndInterpreter: thread still has a frame");
645 if (tstate != interp->tstate_head || tstate->next != NULL)
646 Py_FatalError("Py_EndInterpreter: not the last thread");
647
648 PyImport_Cleanup();
649 PyInterpreterState_Clear(interp);
650 PyThreadState_Swap(NULL);
651 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000652}
653
654static char *progname = "python";
655
656void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000657Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000658{
659 if (pn && *pn)
660 progname = pn;
661}
662
663char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000664Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000665{
666 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000667}
668
Guido van Rossuma61691e1998-02-06 22:27:24 +0000669static char *default_home = NULL;
670
671void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000672Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000673{
674 default_home = home;
675}
676
677char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000678Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000679{
680 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000681 if (home == NULL && !Py_IgnoreEnvironmentFlag)
682 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000683 return home;
684}
685
Guido van Rossum6135a871995-01-09 17:53:26 +0000686/* Create __main__ module */
687
688static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000689initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000690{
Guido van Rossum82598051997-03-05 00:20:32 +0000691 PyObject *m, *d;
692 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000693 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000694 Py_FatalError("can't create __main__ module");
695 d = PyModule_GetDict(m);
696 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000697 PyObject *bimod = PyImport_ImportModule("__builtin__");
698 if (bimod == NULL ||
699 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000700 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000701 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000702 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000703}
704
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000705/* Import the site module (not into __main__ though) */
706
707static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000708initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000709{
710 PyObject *m, *f;
711 m = PyImport_ImportModule("site");
712 if (m == NULL) {
713 f = PySys_GetObject("stderr");
714 if (Py_VerboseFlag) {
715 PyFile_WriteString(
716 "'import site' failed; traceback:\n", f);
717 PyErr_Print();
718 }
719 else {
720 PyFile_WriteString(
721 "'import site' failed; use -v for traceback\n", f);
722 PyErr_Clear();
723 }
724 }
725 else {
726 Py_DECREF(m);
727 }
728}
729
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730/* Parse input from a file and execute it */
731
732int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000733PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000734 PyCompilerFlags *flags)
735{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000736 if (filename == NULL)
737 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000738 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000739 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000740 if (closeit)
741 fclose(fp);
742 return err;
743 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000744 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000745 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000746}
747
748int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000749PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000750{
Guido van Rossum82598051997-03-05 00:20:32 +0000751 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000752 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000753 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000754
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000755 if (flags == NULL) {
756 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000757 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000758 }
Guido van Rossum82598051997-03-05 00:20:32 +0000759 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000760 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000761 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000762 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000763 }
Guido van Rossum82598051997-03-05 00:20:32 +0000764 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000765 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000766 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000767 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000768 }
769 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000770 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000771 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000772 if (ret == E_EOF)
773 return 0;
774 /*
775 if (ret == E_NOMEM)
776 return -1;
777 */
778 }
779}
780
Eric Smith7c478942008-03-18 23:45:49 +0000781#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000782/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000783#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000784 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000785 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000786#endif
787#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000788/* Keep an example of flags with future keyword support. */
789#define PARSER_FLAGS(flags) \
790 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000791 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000792 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
793 PyPARSE_PRINT_IS_FUNCTION : 0) \
794 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
795 PyPARSE_UNICODE_LITERALS : 0) \
796 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000797#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000798
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000799int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000800PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000801{
Guido van Rossum82598051997-03-05 00:20:32 +0000802 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000804 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000805 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000807
Guido van Rossum82598051997-03-05 00:20:32 +0000808 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000809 if (v != NULL) {
810 v = PyObject_Str(v);
811 if (v == NULL)
812 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000813 else if (PyString_Check(v))
814 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000815 }
Guido van Rossum82598051997-03-05 00:20:32 +0000816 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000817 if (w != NULL) {
818 w = PyObject_Str(w);
819 if (w == NULL)
820 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000821 else if (PyString_Check(w))
822 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000823 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000824 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000825 if (arena == NULL) {
826 Py_XDECREF(v);
827 Py_XDECREF(w);
828 return -1;
829 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000830 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000832 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000833 Py_XDECREF(v);
834 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000836 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (errcode == E_EOF) {
838 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000839 return E_EOF;
840 }
Guido van Rossum82598051997-03-05 00:20:32 +0000841 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000843 }
Guido van Rossum82598051997-03-05 00:20:32 +0000844 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000845 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000846 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000847 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000848 }
Guido van Rossum82598051997-03-05 00:20:32 +0000849 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000850 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000851 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000852 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000853 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000854 return -1;
855 }
Guido van Rossum82598051997-03-05 00:20:32 +0000856 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000857 if (Py_FlushLine())
858 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000859 return 0;
860}
861
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000862/* Check whether a file maybe a pyc file: Look at the extension,
863 the file type, and, if we may close it, at the first few bytes. */
864
865static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000866maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000867{
868 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
869 return 1;
870
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000871 /* Only look into the file if we are allowed to close it, since
872 it then should also be seekable. */
873 if (closeit) {
874 /* Read only two bytes of the magic. If the file was opened in
875 text mode, the bytes 3 and 4 of the magic (\r\n) might not
876 be read as they are on disk. */
877 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
878 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000879 /* Mess: In case of -x, the stream is NOT at its start now,
880 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000881 which makes the current stream position formally undefined,
882 and a x-platform nightmare.
883 Unfortunately, we have no direct way to know whether -x
884 was specified. So we use a terrible hack: if the current
885 stream position is not 0, we assume -x was specified, and
886 give up. Bug 132850 on SourceForge spells out the
887 hopelessness of trying anything else (fseek and ftell
888 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000889 */
Tim Peters3e876562001-02-11 04:35:39 +0000890 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000891 if (ftell(fp) == 0) {
892 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000893 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000894 ispyc = 1;
895 rewind(fp);
896 }
Tim Peters3e876562001-02-11 04:35:39 +0000897 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000898 }
899 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000900}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000901
Guido van Rossum0df002c2000-08-27 19:21:52 +0000902int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000903PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000904 PyCompilerFlags *flags)
905{
Guido van Rossum82598051997-03-05 00:20:32 +0000906 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000907 const char *ext;
Matthias Klosea8da9e02009-04-04 14:19:56 +0000908 int set_file_name = 0, ret, len;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000909
Guido van Rossum82598051997-03-05 00:20:32 +0000910 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000911 if (m == NULL)
912 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000913 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000914 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000915 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000916 if (f == NULL)
917 return -1;
918 if (PyDict_SetItemString(d, "__file__", f) < 0) {
919 Py_DECREF(f);
920 return -1;
921 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000922 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000923 Py_DECREF(f);
924 }
Matthias Klosea8da9e02009-04-04 14:19:56 +0000925 len = strlen(filename);
926 ext = filename + len - (len > 4 ? 4 : 0);
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000927 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000928 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000929 if (closeit)
930 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000931 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000932 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000933 ret = -1;
934 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000935 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000936 /* Turn on optimization if a .pyo file is given */
937 if (strcmp(ext, ".pyo") == 0)
938 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000939 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000940 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000941 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000942 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000943 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000944 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000945 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000946 ret = -1;
947 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000948 }
Guido van Rossum82598051997-03-05 00:20:32 +0000949 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000950 if (Py_FlushLine())
951 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000952 ret = 0;
953 done:
954 if (set_file_name && PyDict_DelItemString(d, "__file__"))
955 PyErr_Clear();
956 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000957}
958
959int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000960PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000961{
Guido van Rossum82598051997-03-05 00:20:32 +0000962 PyObject *m, *d, *v;
963 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964 if (m == NULL)
965 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000966 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000967 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000968 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000969 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000970 return -1;
971 }
Guido van Rossum82598051997-03-05 00:20:32 +0000972 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000973 if (Py_FlushLine())
974 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000975 return 0;
976}
977
Barry Warsaw035574d1997-08-29 22:07:17 +0000978static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000979parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
980 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000981{
982 long hold;
983 PyObject *v;
984
985 /* old style errors */
986 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000987 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000988 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000989
990 /* new style errors. `err' is an instance */
991
992 if (! (v = PyObject_GetAttrString(err, "msg")))
993 goto finally;
994 *message = v;
995
996 if (!(v = PyObject_GetAttrString(err, "filename")))
997 goto finally;
998 if (v == Py_None)
999 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001000 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001001 goto finally;
1002
1003 Py_DECREF(v);
1004 if (!(v = PyObject_GetAttrString(err, "lineno")))
1005 goto finally;
1006 hold = PyInt_AsLong(v);
1007 Py_DECREF(v);
1008 v = NULL;
1009 if (hold < 0 && PyErr_Occurred())
1010 goto finally;
1011 *lineno = (int)hold;
1012
1013 if (!(v = PyObject_GetAttrString(err, "offset")))
1014 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001015 if (v == Py_None) {
1016 *offset = -1;
1017 Py_DECREF(v);
1018 v = NULL;
1019 } else {
1020 hold = PyInt_AsLong(v);
1021 Py_DECREF(v);
1022 v = NULL;
1023 if (hold < 0 && PyErr_Occurred())
1024 goto finally;
1025 *offset = (int)hold;
1026 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001027
1028 if (!(v = PyObject_GetAttrString(err, "text")))
1029 goto finally;
1030 if (v == Py_None)
1031 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001032 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001033 goto finally;
1034 Py_DECREF(v);
1035 return 1;
1036
1037finally:
1038 Py_XDECREF(v);
1039 return 0;
1040}
1041
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001042void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001043PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001044{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001045 PyErr_PrintEx(1);
1046}
1047
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001048static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001049print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001050{
1051 char *nl;
1052 if (offset >= 0) {
1053 if (offset > 0 && offset == (int)strlen(text))
1054 offset--;
1055 for (;;) {
1056 nl = strchr(text, '\n');
1057 if (nl == NULL || nl-text >= offset)
1058 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001059 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001060 text = nl+1;
1061 }
1062 while (*text == ' ' || *text == '\t') {
1063 text++;
1064 offset--;
1065 }
1066 }
1067 PyFile_WriteString(" ", f);
1068 PyFile_WriteString(text, f);
1069 if (*text == '\0' || text[strlen(text)-1] != '\n')
1070 PyFile_WriteString("\n", f);
1071 if (offset == -1)
1072 return;
1073 PyFile_WriteString(" ", f);
1074 offset--;
1075 while (offset > 0) {
1076 PyFile_WriteString(" ", f);
1077 offset--;
1078 }
1079 PyFile_WriteString("^\n", f);
1080}
1081
Guido van Rossum66e8e862001-03-23 17:54:43 +00001082static void
1083handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001084{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001085 PyObject *exception, *value, *tb;
1086 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001087
Georg Brandl49aafc92007-03-07 00:34:46 +00001088 if (Py_InspectFlag)
1089 /* Don't exit if -i flag was given. This flag is set to 0
1090 * when entering interactive mode for inspecting. */
1091 return;
1092
Guido van Rossum66e8e862001-03-23 17:54:43 +00001093 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001094 if (Py_FlushLine())
1095 PyErr_Clear();
1096 fflush(stdout);
1097 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001098 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001099 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001100 /* The error code should be in the `code' attribute. */
1101 PyObject *code = PyObject_GetAttrString(value, "code");
1102 if (code) {
1103 Py_DECREF(value);
1104 value = code;
1105 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001106 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001107 }
1108 /* If we failed to dig out the 'code' attribute,
1109 just let the else clause below print the error. */
1110 }
1111 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001112 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001113 else {
1114 PyObject_Print(value, stderr, Py_PRINT_RAW);
1115 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001116 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001117 }
Tim Peterscf615b52003-04-19 18:47:02 +00001118 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001119 /* Restore and clear the exception info, in order to properly decref
1120 * the exception, value, and traceback. If we just exit instead,
1121 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1122 * some finalizers from running.
1123 */
Tim Peterscf615b52003-04-19 18:47:02 +00001124 PyErr_Restore(exception, value, tb);
1125 PyErr_Clear();
1126 Py_Exit(exitcode);
1127 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001128}
1129
1130void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001131PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001132{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001133 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001134
1135 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1136 handle_system_exit();
1137 }
Guido van Rossum82598051997-03-05 00:20:32 +00001138 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 if (exception == NULL)
1140 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001141 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001142 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001143 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001144 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001145 if (set_sys_last_vars) {
1146 PySys_SetObject("last_type", exception);
1147 PySys_SetObject("last_value", v);
1148 PySys_SetObject("last_traceback", tb);
1149 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001150 hook = PySys_GetObject("excepthook");
1151 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001152 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001153 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001154 PyObject *result = PyEval_CallObject(hook, args);
1155 if (result == NULL) {
1156 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001157 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1158 handle_system_exit();
1159 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001160 PyErr_Fetch(&exception2, &v2, &tb2);
1161 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001162 /* It should not be possible for exception2 or v2
1163 to be NULL. However PyErr_Display() can't
1164 tolerate NULLs, so just be safe. */
1165 if (exception2 == NULL) {
1166 exception2 = Py_None;
1167 Py_INCREF(exception2);
1168 }
1169 if (v2 == NULL) {
1170 v2 = Py_None;
1171 Py_INCREF(v2);
1172 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001173 if (Py_FlushLine())
1174 PyErr_Clear();
1175 fflush(stdout);
1176 PySys_WriteStderr("Error in sys.excepthook:\n");
1177 PyErr_Display(exception2, v2, tb2);
1178 PySys_WriteStderr("\nOriginal exception was:\n");
1179 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001180 Py_DECREF(exception2);
1181 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001182 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001183 }
1184 Py_XDECREF(result);
1185 Py_XDECREF(args);
1186 } else {
1187 PySys_WriteStderr("sys.excepthook is missing\n");
1188 PyErr_Display(exception, v, tb);
1189 }
1190 Py_XDECREF(exception);
1191 Py_XDECREF(v);
1192 Py_XDECREF(tb);
1193}
1194
Richard Jones7b9558d2006-05-27 12:29:24 +00001195void
1196PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001197{
1198 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001199 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001200 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001201 if (f == NULL)
1202 fprintf(stderr, "lost sys.stderr\n");
1203 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001204 if (Py_FlushLine())
1205 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001206 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001207 if (tb && tb != Py_None)
1208 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001209 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001210 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001211 {
Guido van Rossum82598051997-03-05 00:20:32 +00001212 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001213 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001214 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001215 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001216 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001217 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001218 else {
1219 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001220 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001221 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001222 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001223 else
Guido van Rossum82598051997-03-05 00:20:32 +00001224 PyFile_WriteString(filename, f);
1225 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001226 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001227 PyFile_WriteString(buf, f);
1228 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001229 if (text != NULL)
1230 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001231 Py_DECREF(value);
1232 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001233 /* Can't be bothered to check all those
1234 PyFile_WriteString() calls */
1235 if (PyErr_Occurred())
1236 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001237 }
1238 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001239 if (err) {
1240 /* Don't do anything else */
1241 }
Brett Cannonbf364092006-03-01 04:25:17 +00001242 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001243 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001244 char* className = PyExceptionClass_Name(exception);
1245 if (className != NULL) {
1246 char *dot = strrchr(className, '.');
1247 if (dot != NULL)
1248 className = dot+1;
1249 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001250
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001251 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001252 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001253 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001254 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001255 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001256 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001257 {
1258 err = PyFile_WriteString(modstr, f);
1259 err += PyFile_WriteString(".", f);
1260 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001261 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001262 }
1263 if (err == 0) {
1264 if (className == NULL)
1265 err = PyFile_WriteString("<unknown>", f);
1266 else
Brett Cannonbf364092006-03-01 04:25:17 +00001267 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001268 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001269 }
1270 else
1271 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001272 if (err == 0 && (value != Py_None)) {
1273 PyObject *s = PyObject_Str(value);
1274 /* only print colon if the str() of the
1275 object is not the empty string
1276 */
1277 if (s == NULL)
1278 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001279 else if (!PyString_Check(s) ||
1280 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001281 err = PyFile_WriteString(": ", f);
1282 if (err == 0)
1283 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1284 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001285 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001286 /* try to write a newline in any case */
1287 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001288 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001289 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001290 /* If an error happened here, don't show it.
1291 XXX This is wrong, but too many callers rely on this behavior. */
1292 if (err != 0)
1293 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001294}
1295
Guido van Rossum82598051997-03-05 00:20:32 +00001296PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001297PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001299{
Neal Norwitze92fba02006-03-04 18:52:26 +00001300 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001301 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001302 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001303 if (arena == NULL)
1304 return NULL;
1305
1306 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001307 if (mod != NULL)
1308 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001309 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001311}
1312
1313PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001314PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001315 PyObject *locals, int closeit, PyCompilerFlags *flags)
1316{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001318 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001319 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001320 if (arena == NULL)
1321 return NULL;
1322
1323 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1324 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001325 if (closeit)
1326 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001328 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001330 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001331 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001332 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001334}
1335
Guido van Rossum82598051997-03-05 00:20:32 +00001336static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001338 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001339{
Guido van Rossum82598051997-03-05 00:20:32 +00001340 PyCodeObject *co;
1341 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001343 if (co == NULL)
1344 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001345 v = PyEval_EvalCode(co, globals, locals);
1346 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001347 return v;
1348}
1349
Guido van Rossum82598051997-03-05 00:20:32 +00001350static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001351run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001353{
Guido van Rossum82598051997-03-05 00:20:32 +00001354 PyCodeObject *co;
1355 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001356 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001357 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001358
Guido van Rossum82598051997-03-05 00:20:32 +00001359 magic = PyMarshal_ReadLongFromFile(fp);
1360 if (magic != PyImport_GetMagicNumber()) {
1361 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001362 "Bad magic number in .pyc file");
1363 return NULL;
1364 }
Guido van Rossum82598051997-03-05 00:20:32 +00001365 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001366 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001367 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001368 if (v == NULL || !PyCode_Check(v)) {
1369 Py_XDECREF(v);
1370 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001371 "Bad code object in .pyc file");
1372 return NULL;
1373 }
Guido van Rossum82598051997-03-05 00:20:32 +00001374 co = (PyCodeObject *)v;
1375 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001376 if (v && flags)
1377 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001378 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001379 return v;
1380}
1381
Guido van Rossum82598051997-03-05 00:20:32 +00001382PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001383Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001384 PyCompilerFlags *flags)
1385{
Guido van Rossum82598051997-03-05 00:20:32 +00001386 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001387 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001388 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001389 if (arena == NULL)
1390 return NULL;
1391
1392 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001393 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001394 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001395 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001396 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001397 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001398 PyObject *result = PyAST_mod2obj(mod);
1399 PyArena_Free(arena);
1400 return result;
1401 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001402 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001403 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001404 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001405}
1406
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001407struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001408Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001409{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001410 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001411 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001412 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001413 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001414 if (arena == NULL)
1415 return NULL;
1416
Christian Heimes7f23d862008-03-26 22:51:58 +00001417 flags.cf_flags = 0;
1418
Christian Heimes3c608332008-03-26 22:01:37 +00001419 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001420 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001421 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001422 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001425 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001426 return st;
1427}
1428
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429/* Preferred access to parser is through AST. */
1430mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001431PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001432 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001435 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001437 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001438
1439 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001440 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001441 &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001442 if (flags == NULL) {
1443 localflags.cf_flags = 0;
1444 flags = &localflags;
1445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001447 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001448 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 PyNode_Free(n);
1450 return mod;
1451 }
1452 else {
1453 err_input(&err);
1454 return NULL;
1455 }
1456}
1457
1458mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001459PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001460 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001461 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 mod_ty mod;
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001464 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001466 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001467
Christian Heimes3c608332008-03-26 22:01:37 +00001468 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1469 start, ps1, ps2, &err, &iflags);
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001470 if (flags == NULL) {
1471 localflags.cf_flags = 0;
1472 flags = &localflags;
1473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 if (n) {
Benjamin Peterson084ce7a2008-10-31 02:26:20 +00001475 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001476 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 PyNode_Free(n);
1478 return mod;
1479 }
1480 else {
1481 err_input(&err);
1482 if (errcode)
1483 *errcode = err.error;
1484 return NULL;
1485 }
1486}
1487
Guido van Rossuma110aa61994-08-29 12:50:44 +00001488/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001489
Guido van Rossuma110aa61994-08-29 12:50:44 +00001490node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001491PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001492{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001493 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001494 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1495 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001496 if (n == NULL)
1497 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001498
Guido van Rossuma110aa61994-08-29 12:50:44 +00001499 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001500}
1501
Guido van Rossuma110aa61994-08-29 12:50:44 +00001502/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001503
Guido van Rossuma110aa61994-08-29 12:50:44 +00001504node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001505PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001506{
Tim Petersfe2127d2001-07-16 05:37:24 +00001507 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001508 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1509 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001510 if (n == NULL)
1511 err_input(&err);
1512 return n;
1513}
1514
1515node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001516PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001517 int start, int flags)
1518{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001519 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001520 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1521 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001522 if (n == NULL)
1523 err_input(&err);
1524 return n;
1525}
1526
1527node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001528PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001529{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001530 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001531}
1532
Guido van Rossum66ebd912003-04-17 16:02:26 +00001533/* May want to move a more generalized form of this to parsetok.c or
1534 even parser modules. */
1535
1536void
1537PyParser_SetError(perrdetail *err)
1538{
1539 err_input(err);
1540}
1541
Guido van Rossuma110aa61994-08-29 12:50:44 +00001542/* Set the error appropriate to the given input error code (see errcode.h) */
1543
1544static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001545err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001546{
Fred Drake85f36392000-07-11 17:53:00 +00001547 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001548 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001549 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001550 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001551 switch (err->error) {
Victor Stinnerea164292010-03-21 14:02:32 +00001552 case E_ERROR:
1553 return;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001554 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001555 errtype = PyExc_IndentationError;
1556 if (err->expected == INDENT)
1557 msg = "expected an indented block";
1558 else if (err->token == INDENT)
1559 msg = "unexpected indent";
1560 else if (err->token == DEDENT)
1561 msg = "unexpected unindent";
1562 else {
1563 errtype = PyExc_SyntaxError;
1564 msg = "invalid syntax";
1565 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001566 break;
1567 case E_TOKEN:
1568 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001569 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001570 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001571 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001572 break;
1573 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001574 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001575 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001576 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001577 if (!PyErr_Occurred())
1578 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001579 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001580 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001581 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001582 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001583 case E_EOF:
1584 msg = "unexpected EOF while parsing";
1585 break;
Fred Drake85f36392000-07-11 17:53:00 +00001586 case E_TABSPACE:
1587 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001588 msg = "inconsistent use of tabs and spaces in indentation";
1589 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001590 case E_OVERFLOW:
1591 msg = "expression too long";
1592 break;
Fred Drake85f36392000-07-11 17:53:00 +00001593 case E_DEDENT:
1594 errtype = PyExc_IndentationError;
1595 msg = "unindent does not match any outer indentation level";
1596 break;
1597 case E_TOODEEP:
1598 errtype = PyExc_IndentationError;
1599 msg = "too many levels of indentation";
1600 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001601 case E_DECODE: {
1602 PyObject *type, *value, *tb;
1603 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001604 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001605 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001606 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001607 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001608 }
1609 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001610 if (msg == NULL)
1611 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001612 Py_XDECREF(type);
1613 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001614 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001615 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001616 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001617 case E_LINECONT:
1618 msg = "unexpected character after line continuation character";
1619 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001620 default:
1621 fprintf(stderr, "error=%d\n", err->error);
1622 msg = "unknown parsing error";
1623 break;
1624 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 v = Py_BuildValue("(ziiz)", err->filename,
1626 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 w = NULL;
1628 if (v != NULL)
1629 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001630 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001631 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001632 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001633 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001634cleanup:
1635 if (err->text != NULL) {
1636 PyObject_FREE(err->text);
1637 err->text = NULL;
1638 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001639}
1640
1641/* Print fatal error message and abort */
1642
1643void
Tim Peters7c321a82002-07-09 02:57:01 +00001644Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001645{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001646 fprintf(stderr, "Fatal Python error: %s\n", msg);
Jesse Noller70b11f02009-03-31 22:25:20 +00001647 fflush(stderr); /* it helps in Windows debug build */
1648
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001649#ifdef MS_WINDOWS
Georg Brandl734373c2009-01-03 21:55:17 +00001650 {
1651 size_t len = strlen(msg);
1652 WCHAR* buffer;
1653 size_t i;
1654
1655 /* Convert the message to wchar_t. This uses a simple one-to-one
1656 conversion, assuming that the this error message actually uses ASCII
1657 only. If this ceases to be true, we will have to convert. */
1658 buffer = alloca( (len+1) * (sizeof *buffer));
1659 for( i=0; i<=len; ++i)
1660 buffer[i] = msg[i];
1661 OutputDebugStringW(L"Fatal Python error: ");
1662 OutputDebugStringW(buffer);
1663 OutputDebugStringW(L"\n");
1664 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001665#ifdef _DEBUG
1666 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001667#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001668#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001669 abort();
1670}
1671
1672/* Clean up and exit */
1673
Guido van Rossuma110aa61994-08-29 12:50:44 +00001674#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001675#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001676#endif
1677
Antoine Pitrou9aece752009-10-27 12:48:52 +00001678/* Wait until threading._shutdown completes, provided
1679 the threading module was imported in the first place.
1680 The shutdown routine will wait until all non-daemon
1681 "threading" threads have completed. */
1682static void
1683wait_for_thread_shutdown(void)
1684{
1685#ifdef WITH_THREAD
1686 PyObject *result;
1687 PyThreadState *tstate = PyThreadState_GET();
1688 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1689 "threading");
1690 if (threading == NULL) {
1691 /* threading not imported */
1692 PyErr_Clear();
1693 return;
1694 }
1695 result = PyObject_CallMethod(threading, "_shutdown", "");
1696 if (result == NULL)
1697 PyErr_WriteUnraisable(threading);
1698 else
1699 Py_DECREF(result);
1700 Py_DECREF(threading);
1701#endif
1702}
1703
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001704#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001705static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001706static int nexitfuncs = 0;
1707
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001708int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001709{
1710 if (nexitfuncs >= NEXITFUNCS)
1711 return -1;
1712 exitfuncs[nexitfuncs++] = func;
1713 return 0;
1714}
1715
Guido van Rossumcc283f51997-08-05 02:22:03 +00001716static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001717call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001718{
Guido van Rossum82598051997-03-05 00:20:32 +00001719 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001720
1721 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001722 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001723 Py_INCREF(exitfunc);
1724 PySys_SetObject("exitfunc", (PyObject *)NULL);
1725 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001726 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001727 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1728 PySys_WriteStderr("Error in sys.exitfunc:\n");
1729 }
Guido van Rossum82598051997-03-05 00:20:32 +00001730 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001731 }
Guido van Rossum82598051997-03-05 00:20:32 +00001732 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001733 }
1734
Guido van Rossum0829c751998-02-28 04:31:39 +00001735 if (Py_FlushLine())
1736 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001737}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001738
Guido van Rossumcc283f51997-08-05 02:22:03 +00001739static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001740call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001741{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001742 while (nexitfuncs > 0)
1743 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001744
1745 fflush(stdout);
1746 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001747}
1748
1749void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001750Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001751{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001752 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001753
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001754 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001755}
1756
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001757static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001758initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001759{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001760#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001761 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001762#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001763#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001764 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001765#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001766#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001767 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001768#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001769 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001770}
1771
Guido van Rossum7433b121997-02-14 19:45:36 +00001772
1773/*
1774 * The file descriptor fd is considered ``interactive'' if either
1775 * a) isatty(fd) is TRUE, or
1776 * b) the -i flag was given, and the filename associated with
1777 * the descriptor is NULL or "<stdin>" or "???".
1778 */
1779int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001780Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001781{
1782 if (isatty((int)fileno(fp)))
1783 return 1;
1784 if (!Py_InteractiveFlag)
1785 return 0;
1786 return (filename == NULL) ||
1787 (strcmp(filename, "<stdin>") == 0) ||
1788 (strcmp(filename, "???") == 0);
1789}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001790
1791
Tim Petersd08e3822003-04-17 15:24:21 +00001792#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001793#if defined(WIN32) && defined(_MSC_VER)
1794
1795/* Stack checking for Microsoft C */
1796
1797#include <malloc.h>
1798#include <excpt.h>
1799
Fred Drakee8de31c2000-08-31 05:38:39 +00001800/*
1801 * Return non-zero when we run out of memory on the stack; zero otherwise.
1802 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001803int
Fred Drake399739f2000-08-31 05:52:44 +00001804PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001805{
1806 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001807 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001808 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001809 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001810 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001811 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1812 EXCEPTION_EXECUTE_HANDLER :
1813 EXCEPTION_CONTINUE_SEARCH) {
1814 int errcode = _resetstkoflw();
Amaury Forgeot d'Arc74b458c2008-11-22 20:06:51 +00001815 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001816 {
1817 Py_FatalError("Could not reset the stack!");
1818 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001819 }
1820 return 1;
1821}
1822
1823#endif /* WIN32 && _MSC_VER */
1824
1825/* Alternate implementations can be added here... */
1826
1827#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001828
1829
1830/* Wrappers around sigaction() or signal(). */
1831
1832PyOS_sighandler_t
1833PyOS_getsig(int sig)
1834{
1835#ifdef HAVE_SIGACTION
1836 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001837 if (sigaction(sig, NULL, &context) == -1)
1838 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001839 return context.sa_handler;
1840#else
1841 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001842/* Special signal handling for the secure CRT in Visual Studio 2005 */
1843#if defined(_MSC_VER) && _MSC_VER >= 1400
1844 switch (sig) {
1845 /* Only these signals are valid */
1846 case SIGINT:
1847 case SIGILL:
1848 case SIGFPE:
1849 case SIGSEGV:
1850 case SIGTERM:
1851 case SIGBREAK:
1852 case SIGABRT:
1853 break;
1854 /* Don't call signal() with other values or it will assert */
1855 default:
1856 return SIG_ERR;
1857 }
1858#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001859 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001860 if (handler != SIG_ERR)
1861 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001862 return handler;
1863#endif
1864}
1865
1866PyOS_sighandler_t
1867PyOS_setsig(int sig, PyOS_sighandler_t handler)
1868{
1869#ifdef HAVE_SIGACTION
Jean-Paul Calderone0819b092010-05-08 21:11:28 +00001870 /* Some code in Modules/signalmodule.c depends on sigaction() being
1871 * used here if HAVE_SIGACTION is defined. Fix that if this code
1872 * changes to invalidate that assumption.
1873 */
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001874 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001875 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001876 sigemptyset(&context.sa_mask);
1877 context.sa_flags = 0;
1878 if (sigaction(sig, &context, &ocontext) == -1)
1879 return SIG_ERR;
1880 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001881#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001882 PyOS_sighandler_t oldhandler;
1883 oldhandler = signal(sig, handler);
1884#ifdef HAVE_SIGINTERRUPT
1885 siginterrupt(sig, 1);
1886#endif
1887 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001888#endif
1889}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890
1891/* Deprecated C API functions still provided for binary compatiblity */
1892
1893#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001894PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1896{
1897 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1898}
1899
Thomas Heller1b046642006-04-18 18:51:06 +00001900#undef PyParser_SimpleParseString
1901PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902PyParser_SimpleParseString(const char *str, int start)
1903{
1904 return PyParser_SimpleParseStringFlags(str, start, 0);
1905}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001906
Thomas Heller1b046642006-04-18 18:51:06 +00001907#undef PyRun_AnyFile
1908PyAPI_FUNC(int)
1909PyRun_AnyFile(FILE *fp, const char *name)
1910{
1911 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1912}
1913
1914#undef PyRun_AnyFileEx
1915PyAPI_FUNC(int)
1916PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1917{
1918 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1919}
1920
1921#undef PyRun_AnyFileFlags
1922PyAPI_FUNC(int)
1923PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1924{
1925 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1926}
1927
1928#undef PyRun_File
1929PyAPI_FUNC(PyObject *)
1930PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1931{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001932 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001933}
1934
1935#undef PyRun_FileEx
1936PyAPI_FUNC(PyObject *)
1937PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1938{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001939 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001940}
1941
1942#undef PyRun_FileFlags
1943PyAPI_FUNC(PyObject *)
1944PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1945 PyCompilerFlags *flags)
1946{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001947 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001948}
1949
1950#undef PyRun_SimpleFile
1951PyAPI_FUNC(int)
1952PyRun_SimpleFile(FILE *f, const char *p)
1953{
1954 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1955}
1956
1957#undef PyRun_SimpleFileEx
1958PyAPI_FUNC(int)
1959PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1960{
1961 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1962}
1963
1964
1965#undef PyRun_String
1966PyAPI_FUNC(PyObject *)
1967PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1968{
1969 return PyRun_StringFlags(str, s, g, l, NULL);
1970}
1971
1972#undef PyRun_SimpleString
1973PyAPI_FUNC(int)
1974PyRun_SimpleString(const char *s)
1975{
1976 return PyRun_SimpleStringFlags(s, NULL);
1977}
1978
1979#undef Py_CompileString
1980PyAPI_FUNC(PyObject *)
1981Py_CompileString(const char *str, const char *p, int s)
1982{
1983 return Py_CompileStringFlags(str, p, s, NULL);
1984}
1985
1986#undef PyRun_InteractiveOne
1987PyAPI_FUNC(int)
1988PyRun_InteractiveOne(FILE *f, const char *p)
1989{
1990 return PyRun_InteractiveOneFlags(f, p, NULL);
1991}
1992
1993#undef PyRun_InteractiveLoop
1994PyAPI_FUNC(int)
1995PyRun_InteractiveLoop(FILE *f, const char *p)
1996{
1997 return PyRun_InteractiveLoopFlags(f, p, NULL);
1998}
1999
Anthony Baxterac6bd462006-04-13 02:06:09 +00002000#ifdef __cplusplus
2001}
2002#endif
2003