blob: f95ea831aa877ad3e54944c34eaa569f6d964cb2 [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"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020
Thomas Wouters0e3f5912006-08-11 14:57:12 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000024
Martin v. Löwis73d538b2003-03-05 15:13:47 +000025#ifdef HAVE_LANGINFO_H
26#include <locale.h>
27#include <langinfo.h>
28#endif
29
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000030#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000031#undef BYTE
32#include "windows.h"
33#endif
34
Neal Norwitz4281cef2006-03-04 19:58:13 +000035#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#else /* Py_REF_DEBUG */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038#define PRINT_TOTAL_REFS() fprintf(stderr, \
39 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
40 _Py_GetRefTotal())
41#endif
42
43#ifdef __cplusplus
44extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000045#endif
46
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000047extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000048
Guido van Rossum82598051997-03-05 00:20:32 +000049extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000050
Guido van Rossumb73cc041993-11-01 16:28:59 +000051/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000052static void initmain(void);
53static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000054static int initstdio(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000056 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000057static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000058 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000059static void err_input(perrdetail *);
60static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000061static void call_py_exitfuncs(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000062static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000063extern void _PyUnicode_Init(void);
64extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +000065extern int _PyLong_Init(void);
66extern void PyLong_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000067
Mark Hammond8d98d2c2003-04-19 15:41:53 +000068#ifdef WITH_THREAD
69extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
70extern void _PyGILState_Fini(void);
71#endif /* WITH_THREAD */
72
Guido van Rossum82598051997-03-05 00:20:32 +000073int Py_DebugFlag; /* Needed by parser.c */
74int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000075int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Guido van Rossumd8faa362007-04-27 19:54:29 +000076int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000077int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +000078int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000079int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000080int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000081int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000082
Mark Hammonda43fd0c2003-02-19 00:33:33 +000083/* Reference to 'warnings' module, to avoid importing it
Mark Hammondedd07732003-07-15 23:03:55 +000084 on the fly when the import lock may be held. See 683658/771097
Mark Hammonda43fd0c2003-02-19 00:33:33 +000085*/
Mark Hammondedd07732003-07-15 23:03:55 +000086static PyObject *warnings_module = NULL;
87
88/* Returns a borrowed reference to the 'warnings' module, or NULL.
89 If the module is returned, it is guaranteed to have been obtained
90 without acquiring the import lock
91*/
Martin v. Löwisa2c17c52003-08-09 09:47:11 +000092PyObject *PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000093{
94 PyObject *typ, *val, *tb;
95 PyObject *all_modules;
96 /* If we managed to get the module at init time, just use it */
97 if (warnings_module)
98 return warnings_module;
99 /* If it wasn't available at init time, it may be available
100 now in sys.modules (common scenario is frozen apps: import
101 at init time fails, but the frozen init code sets up sys.path
102 correctly, then does an implicit import of warnings for us
103 */
104 /* Save and restore any exceptions */
105 PyErr_Fetch(&typ, &val, &tb);
106
Mark Hammond5f4e8ca2003-07-16 01:54:38 +0000107 all_modules = PySys_GetObject("modules");
Mark Hammondedd07732003-07-15 23:03:55 +0000108 if (all_modules) {
109 warnings_module = PyDict_GetItemString(all_modules, "warnings");
110 /* We keep a ref in the global */
111 Py_XINCREF(warnings_module);
112 }
113 PyErr_Restore(typ, val, tb);
114 return warnings_module;
115}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000118
Thomas Wouters7e474022000-07-16 12:04:32 +0000119/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000120
121int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000122Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000123{
124 return initialized;
125}
126
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127/* Global initializations. Can be undone by Py_Finalize(). Don't
128 call this twice without an intervening Py_Finalize() call. When
129 initializations fail, a fatal error is issued and the function does
130 not return. On return, the first thread and interpreter state have
131 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000132
Guido van Rossum25ce5661997-08-02 03:10:38 +0000133 Locking: you must hold the interpreter lock while calling this.
134 (If the lock has not yet been initialized, that's equivalent to
135 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000136
Guido van Rossum25ce5661997-08-02 03:10:38 +0000137*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000138
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000139static int
140add_flag(int flag, const char *envs)
141{
142 int env = atoi(envs);
143 if (flag < env)
144 flag = env;
145 if (flag < 1)
146 flag = 1;
147 return flag;
148}
149
Guido van Rossuma027efa1997-05-05 20:56:21 +0000150void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000151Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000152{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000153 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000154 PyThreadState *tstate;
Guido van Rossum826d8972007-10-30 18:34:07 +0000155 PyObject *bimod, *sysmod, *pstderr;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000156 char *p;
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000157#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000158 char *codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000159#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000160 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000161
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000162 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000163 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000164 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000165
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000166#ifdef HAVE_SETLOCALE
167 /* Set up the LC_CTYPE locale, so we can obtain
168 the locale's charset without having to switch
169 locales. */
170 setlocale(LC_CTYPE, "");
171#endif
172
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000173 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000174 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000175 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000176 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000177 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000178 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000179
Guido van Rossuma027efa1997-05-05 20:56:21 +0000180 interp = PyInterpreterState_New();
181 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000182 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000183
Guido van Rossuma027efa1997-05-05 20:56:21 +0000184 tstate = PyThreadState_New(interp);
185 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000187 (void) PyThreadState_Swap(tstate);
188
Guido van Rossum70d893a2001-08-16 08:21:42 +0000189 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000190
Neal Norwitzb2501f42002-12-31 03:42:13 +0000191 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000192 Py_FatalError("Py_Initialize: can't init frames");
193
Guido van Rossumddefaf32007-01-14 03:31:43 +0000194 if (!_PyLong_Init())
195 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000196
Neal Norwitz6968b052007-02-27 19:02:19 +0000197 if (!PyBytes_Init())
198 Py_FatalError("Py_Initialize: can't init bytes");
199
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000200 _PyFloat_Init();
201
Guido van Rossum25ce5661997-08-02 03:10:38 +0000202 interp->modules = PyDict_New();
203 if (interp->modules == NULL)
204 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossumd8faa362007-04-27 19:54:29 +0000205 interp->modules_reloading = PyDict_New();
206 if (interp->modules_reloading == NULL)
207 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000208
Guido van Rossumc94044c2000-03-10 23:03:54 +0000209 /* Init Unicode implementation; relies on the codec registry */
210 _PyUnicode_Init();
211
Barry Warsawf242aa02000-05-25 23:09:49 +0000212 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000213 if (bimod == NULL)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000214 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000215 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000216 if (interp->builtins == NULL)
217 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000218 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000219
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000220 /* initialize builtin exceptions */
221 _PyExc_Init();
222
Guido van Rossum25ce5661997-08-02 03:10:38 +0000223 sysmod = _PySys_Init();
224 if (sysmod == NULL)
225 Py_FatalError("Py_Initialize: can't initialize sys");
226 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000227 if (interp->sysdict == NULL)
228 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000229 Py_INCREF(interp->sysdict);
230 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000231 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000232 PyDict_SetItemString(interp->sysdict, "modules",
233 interp->modules);
234
Guido van Rossum826d8972007-10-30 18:34:07 +0000235 /* Set up a preliminary stderr printer until we have enough
236 infrastructure for the io module in place. */
237 pstderr = PyFile_NewStdPrinter(fileno(stderr));
238 if (pstderr == NULL)
239 Py_FatalError("Py_Initialize: can't set preliminary stderr");
240 PySys_SetObject("stderr", pstderr);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000241 PySys_SetObject("__stderr__", pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000242
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000243 _PyImport_Init();
244
Barry Warsaw035574d1997-08-29 22:07:17 +0000245 /* phase 2 of builtins */
Georg Brandl1a3284e2007-12-02 09:40:06 +0000246 _PyImport_FixupExtension("builtins", "builtins");
Barry Warsaw035574d1997-08-29 22:07:17 +0000247
Just van Rossum52e14d62002-12-30 22:08:05 +0000248 _PyImportHooks_Init();
249
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000250 if (install_sigs)
251 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000252
253 initmain(); /* Module __main__ */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000254 if (initstdio() < 0)
255 Py_FatalError(
256 "Py_Initialize: can't initialize sys standard streams");
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000257 if (!Py_NoSiteFlag)
258 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000259
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000260 /* auto-thread-state API, if available */
261#ifdef WITH_THREAD
262 _PyGILState_Init(interp, tstate);
263#endif /* WITH_THREAD */
264
Mark Hammondedd07732003-07-15 23:03:55 +0000265 warnings_module = PyImport_ImportModule("warnings");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000266 if (!warnings_module) {
Mark Hammondedd07732003-07-15 23:03:55 +0000267 PyErr_Clear();
Guido van Rossum98297ee2007-11-06 21:34:58 +0000268 }
269 else {
270 PyObject *o;
271 char *action[8];
272
273 if (Py_BytesWarningFlag > 1)
274 *action = "error";
275 else if (Py_BytesWarningFlag)
276 *action = "default";
277 else
278 *action = "ignore";
279
280 o = PyObject_CallMethod(warnings_module,
281 "simplefilter", "sO",
282 *action, PyExc_BytesWarning);
283 if (o == NULL)
284 Py_FatalError("Py_Initialize: can't initialize"
285 "warning filter for BytesWarning.");
286 Py_DECREF(o);
287 }
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000288
Guido van Rossum8d30cc02007-05-03 17:49:24 +0000289#if defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000290 /* On Unix, set the file system encoding according to the
291 user's preference, if the CODESET names a well-known
292 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000293 initialized by other means. Also set the encoding of
294 stdin and stdout if these are terminals. */
295
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000296 codeset = nl_langinfo(CODESET);
297 if (codeset && *codeset) {
298 PyObject *enc = PyCodec_Encoder(codeset);
299 if (enc) {
300 codeset = strdup(codeset);
301 Py_DECREF(enc);
302 } else {
303 codeset = NULL;
304 PyErr_Clear();
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000305 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000306 } else
307 codeset = NULL;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000308
309 if (codeset) {
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000310 if (!Py_FileSystemDefaultEncoding)
311 Py_FileSystemDefaultEncoding = codeset;
312 else
313 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000314 }
315#endif
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316}
317
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000318void
319Py_Initialize(void)
320{
321 Py_InitializeEx(1);
322}
323
324
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000325#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000327#endif
328
Guido van Rossume8432ac2007-07-09 15:04:50 +0000329/* Flush stdout and stderr */
330
Neal Norwitz2bad9702007-08-27 06:19:22 +0000331static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000332flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000333{
334 PyObject *fout = PySys_GetObject("stdout");
335 PyObject *ferr = PySys_GetObject("stderr");
336 PyObject *tmp;
337
Christian Heimes2be03732007-11-15 02:26:46 +0000338 if (fout != NULL && fout != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000339 tmp = PyObject_CallMethod(fout, "flush", "");
340 if (tmp == NULL)
341 PyErr_Clear();
342 else
343 Py_DECREF(tmp);
344 }
345
Christian Heimes2be03732007-11-15 02:26:46 +0000346 if (ferr != NULL || ferr != Py_None) {
Guido van Rossume8432ac2007-07-09 15:04:50 +0000347 tmp = PyObject_CallMethod(ferr, "flush", "");
348 if (tmp == NULL)
349 PyErr_Clear();
350 else
351 Py_DECREF(tmp);
352 }
353}
354
Guido van Rossum25ce5661997-08-02 03:10:38 +0000355/* Undo the effect of Py_Initialize().
356
357 Beware: if multiple interpreter and/or thread states exist, these
358 are not wiped out; only the current thread and interpreter state
359 are deleted. But since everything else is deleted, those other
360 interpreter and thread states should no longer be used.
361
362 (XXX We should do better, e.g. wipe out all interpreters and
363 threads.)
364
365 Locking: as above.
366
367*/
368
369void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000370Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000371{
372 PyInterpreterState *interp;
373 PyThreadState *tstate;
374
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000375 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000376 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000377
Tim Peters384fd102001-01-21 03:40:37 +0000378 /* The interpreter is still entirely intact at this point, and the
379 * exit funcs may be relying on that. In particular, if some thread
380 * or exit func is still waiting to do an import, the import machinery
381 * expects Py_IsInitialized() to return true. So don't say the
382 * interpreter is uninitialized until after the exit funcs have run.
383 * Note that Threading.py uses an exit func to do a join on all the
384 * threads created thru it, so this also protects pending imports in
385 * the threads created via Threading.
386 */
Collin Winter670e6922007-03-21 02:57:17 +0000387 call_py_exitfuncs();
Tim Peters384fd102001-01-21 03:40:37 +0000388 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000389
Guido van Rossume8432ac2007-07-09 15:04:50 +0000390 /* Flush stdout+stderr */
391 flush_std_files();
392
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000393 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000394 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000395 interp = tstate->interp;
396
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000397 /* Disable signal handling */
398 PyOS_FiniInterrupts();
399
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000400 /* drop module references we saved */
Mark Hammondedd07732003-07-15 23:03:55 +0000401 Py_XDECREF(warnings_module);
402 warnings_module = NULL;
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000403
Guido van Rossume13ddc92003-04-17 17:29:22 +0000404 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000405 * before all modules are destroyed.
406 * XXX If a __del__ or weakref callback is triggered here, and tries to
407 * XXX import a module, bad things can happen, because Python no
408 * XXX longer believes it's initialized.
409 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
410 * XXX is easy to provoke that way. I've also seen, e.g.,
411 * XXX Exception exceptions.ImportError: 'No module named sha'
412 * XXX in <function callback at 0x008F5718> ignored
413 * XXX but I'm unclear on exactly how that one happens. In any case,
414 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000415 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000416 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000417#ifdef COUNT_ALLOCS
418 /* With COUNT_ALLOCS, it helps to run GC multiple times:
419 each collection might release some types from the type
420 list, so they become garbage. */
421 while (PyGC_Collect() > 0)
422 /* nothing */;
423#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000424
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000425 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000426 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000427
Guido van Rossume8432ac2007-07-09 15:04:50 +0000428 /* Flush stdout+stderr (again, in case more was printed) */
429 flush_std_files();
430
Guido van Rossume13ddc92003-04-17 17:29:22 +0000431 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000432 * new-style class definitions, for example.
433 * XXX This is disabled because it caused too many problems. If
434 * XXX a __del__ or weakref callback triggers here, Python code has
435 * XXX a hard time running, because even the sys module has been
436 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
437 * XXX One symptom is a sequence of information-free messages
438 * XXX coming from threads (if a __del__ or callback is invoked,
439 * XXX other threads can execute too, and any exception they encounter
440 * XXX triggers a comedy of errors as subsystem after subsystem
441 * XXX fails to find what it *expects* to find in sys to help report
442 * XXX the exception and consequent unexpected failures). I've also
443 * XXX seen segfaults then, after adding print statements to the
444 * XXX Python code getting called.
445 */
446#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000447 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000448#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000449
Guido van Rossum1707aad1997-12-08 23:43:45 +0000450 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
451 _PyImport_Fini();
452
453 /* Debugging stuff */
454#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000456#endif
457
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000459
Tim Peters9cf25ce2003-04-17 15:21:01 +0000460#ifdef Py_TRACE_REFS
461 /* Display all objects still alive -- this can invoke arbitrary
462 * __repr__ overrides, so requires a mostly-intact interpreter.
463 * Alas, a lot of stuff may still be alive now that will be cleaned
464 * up later.
465 */
Tim Peters269b2a62003-04-17 19:52:29 +0000466 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000467 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000468#endif /* Py_TRACE_REFS */
469
Guido van Rossumd922fa42003-04-15 14:10:09 +0000470 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000471 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000472
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000473 /* Now we decref the exception classes. After this point nothing
474 can raise an exception. That's okay, because each Fini() method
475 below has been checked to make sure no exceptions are ever
476 raised.
477 */
478
479 _PyExc_Fini();
480
Christian Heimes7d2ff882007-11-30 14:35:04 +0000481 /* Cleanup auto-thread-state */
482#ifdef WITH_THREAD
483 _PyGILState_Fini();
484#endif /* WITH_THREAD */
485
Guido van Rossumd922fa42003-04-15 14:10:09 +0000486 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000487 PyThreadState_Swap(NULL);
488 PyInterpreterState_Delete(interp);
489
Guido van Rossumd922fa42003-04-15 14:10:09 +0000490 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000491 PyMethod_Fini();
492 PyFrame_Fini();
493 PyCFunction_Fini();
494 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000495 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000496 PySet_Fini();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000497 PyString_Fini();
Neal Norwitz6968b052007-02-27 19:02:19 +0000498 PyBytes_Fini();
Guido van Rossumddefaf32007-01-14 03:31:43 +0000499 PyLong_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000500 PyFloat_Fini();
501
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000502 /* Cleanup Unicode implementation */
503 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000504
Christian Heimesc8967002007-11-30 10:18:26 +0000505 /* reset file system default encoding */
506 if (!Py_HasFileSystemDefaultEncoding) {
507 free((char*)Py_FileSystemDefaultEncoding);
508 Py_FileSystemDefaultEncoding = NULL;
509 }
510
Guido van Rossumcc283f51997-08-05 02:22:03 +0000511 /* XXX Still allocated:
512 - various static ad-hoc pointers to interned strings
513 - int and float free list blocks
514 - whatever various modules and libraries allocate
515 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000516
517 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000518
Tim Peters269b2a62003-04-17 19:52:29 +0000519#ifdef Py_TRACE_REFS
520 /* Display addresses (& refcnts) of all objects still alive.
521 * An address can be used to find the repr of the object, printed
522 * above by _Py_PrintReferences.
523 */
524 if (Py_GETENV("PYTHONDUMPREFS"))
525 _Py_PrintReferenceAddresses(stderr);
526#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000527#ifdef PYMALLOC_DEBUG
528 if (Py_GETENV("PYTHONMALLOCSTATS"))
529 _PyObject_DebugMallocStats();
530#endif
531
Guido van Rossumcc283f51997-08-05 02:22:03 +0000532 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000533}
534
535/* Create and initialize a new interpreter and thread, and return the
536 new thread. This requires that Py_Initialize() has been called
537 first.
538
539 Unsuccessful initialization yields a NULL pointer. Note that *no*
540 exception information is available even in this case -- the
541 exception information is held in the thread, and there is no
542 thread.
543
544 Locking: as above.
545
546*/
547
548PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000550{
551 PyInterpreterState *interp;
552 PyThreadState *tstate, *save_tstate;
553 PyObject *bimod, *sysmod;
554
555 if (!initialized)
556 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
557
558 interp = PyInterpreterState_New();
559 if (interp == NULL)
560 return NULL;
561
562 tstate = PyThreadState_New(interp);
563 if (tstate == NULL) {
564 PyInterpreterState_Delete(interp);
565 return NULL;
566 }
567
568 save_tstate = PyThreadState_Swap(tstate);
569
570 /* XXX The following is lax in error checking */
571
572 interp->modules = PyDict_New();
Guido van Rossumd8faa362007-04-27 19:54:29 +0000573 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000574
Georg Brandl1a3284e2007-12-02 09:40:06 +0000575 bimod = _PyImport_FindExtension("builtins", "builtins");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000576 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000577 interp->builtins = PyModule_GetDict(bimod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000578 if (interp->builtins == NULL)
579 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000580 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000581 }
582 sysmod = _PyImport_FindExtension("sys", "sys");
583 if (bimod != NULL && sysmod != NULL) {
584 interp->sysdict = PyModule_GetDict(sysmod);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000585 if (interp->sysdict == NULL)
586 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000587 Py_INCREF(interp->sysdict);
588 PySys_SetPath(Py_GetPath());
589 PyDict_SetItemString(interp->sysdict, "modules",
590 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000591 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000592 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000593 if (!Py_NoSiteFlag)
594 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595 }
596
597 if (!PyErr_Occurred())
598 return tstate;
599
Thomas Wouters89f507f2006-12-13 04:49:30 +0000600handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000601 /* Oops, it didn't work. Undo it all. */
602
603 PyErr_Print();
604 PyThreadState_Clear(tstate);
605 PyThreadState_Swap(save_tstate);
606 PyThreadState_Delete(tstate);
607 PyInterpreterState_Delete(interp);
608
609 return NULL;
610}
611
612/* Delete an interpreter and its last thread. This requires that the
613 given thread state is current, that the thread has no remaining
614 frames, and that it is its interpreter's only remaining thread.
615 It is a fatal error to violate these constraints.
616
617 (Py_Finalize() doesn't have these constraints -- it zaps
618 everything, regardless.)
619
620 Locking: as above.
621
622*/
623
624void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000625Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000626{
627 PyInterpreterState *interp = tstate->interp;
628
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000629 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630 Py_FatalError("Py_EndInterpreter: thread is not current");
631 if (tstate->frame != NULL)
632 Py_FatalError("Py_EndInterpreter: thread still has a frame");
633 if (tstate != interp->tstate_head || tstate->next != NULL)
634 Py_FatalError("Py_EndInterpreter: not the last thread");
635
636 PyImport_Cleanup();
637 PyInterpreterState_Clear(interp);
638 PyThreadState_Swap(NULL);
639 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000640}
641
642static char *progname = "python";
643
644void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000645Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000646{
647 if (pn && *pn)
648 progname = pn;
649}
650
651char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000652Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000653{
654 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000655}
656
Guido van Rossuma61691e1998-02-06 22:27:24 +0000657static char *default_home = NULL;
658
659void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000661{
662 default_home = home;
663}
664
665char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000667{
668 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000669 if (home == NULL && !Py_IgnoreEnvironmentFlag)
670 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000671 return home;
672}
673
Guido van Rossum6135a871995-01-09 17:53:26 +0000674/* Create __main__ module */
675
676static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000677initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000678{
Guido van Rossum82598051997-03-05 00:20:32 +0000679 PyObject *m, *d;
680 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000681 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000682 Py_FatalError("can't create __main__ module");
683 d = PyModule_GetDict(m);
684 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Georg Brandl1a3284e2007-12-02 09:40:06 +0000685 PyObject *bimod = PyImport_ImportModule("builtins");
Guido van Rossum858cb731997-11-19 16:15:37 +0000686 if (bimod == NULL ||
687 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000688 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000689 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000690 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000691}
692
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000693/* Import the site module (not into __main__ though) */
694
695static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000696initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000697{
698 PyObject *m, *f;
699 m = PyImport_ImportModule("site");
700 if (m == NULL) {
701 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000702 if (f == NULL || f == Py_None)
703 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000704 if (Py_VerboseFlag) {
705 PyFile_WriteString(
706 "'import site' failed; traceback:\n", f);
707 PyErr_Print();
708 }
709 else {
710 PyFile_WriteString(
711 "'import site' failed; use -v for traceback\n", f);
712 PyErr_Clear();
713 }
714 }
715 else {
716 Py_DECREF(m);
717 }
718}
719
Georg Brandl1a3284e2007-12-02 09:40:06 +0000720/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000721static int
722initstdio(void)
723{
724 PyObject *iomod = NULL, *wrapper;
725 PyObject *bimod = NULL;
726 PyObject *m;
727 PyObject *std = NULL;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000728 int status = 0, fd;
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000729
730 /* Hack to avoid a nasty recursion issue when Python is invoked
731 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
732 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
733 goto error;
734 }
735 Py_DECREF(m);
736
737 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
738 goto error;
739 }
740 Py_DECREF(m);
741
Georg Brandl1a3284e2007-12-02 09:40:06 +0000742 if (!(bimod = PyImport_ImportModule("builtins"))) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000743 goto error;
744 }
745
746 if (!(iomod = PyImport_ImportModule("io"))) {
747 goto error;
748 }
749 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
750 goto error;
751 }
752
Georg Brandl1a3284e2007-12-02 09:40:06 +0000753 /* Set builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000754 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
755 goto error;
756 }
757
758 /* Set sys.stdin */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000759 fd = fileno(stdin);
760 /* Under some conditions stdin, stdout and stderr may not be connected
761 * and fileno() may point to an invalid file descriptor. For example
762 * GUI apps don't have valid standard streams by default.
763 */
764 if (fd < 0) {
765#ifdef MS_WINDOWS
766 std = Py_None;
767 Py_INCREF(std);
768#else
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000769 goto error;
Christian Heimes58cb1b82007-11-13 02:19:40 +0000770#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000771 }
Christian Heimes58cb1b82007-11-13 02:19:40 +0000772 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000773 if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000774 "\n", 0))) {
775 goto error;
776 }
777 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000778 PySys_SetObject("__stdin__", std);
779 PySys_SetObject("stdin", std);
780 Py_DECREF(std);
781
782 /* Set sys.stdout */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000783 fd = fileno(stdout);
784 if (fd < 0) {
785#ifdef MS_WINDOWS
786 std = Py_None;
787 Py_INCREF(std);
788#else
789 goto error;
790#endif
791 }
792 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000793 if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000794 "\n", 0))) {
795 goto error;
796 }
797 } /* if (fd < 0) */
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000798 PySys_SetObject("__stdout__", std);
799 PySys_SetObject("stdout", std);
800 Py_DECREF(std);
801
Guido van Rossum98297ee2007-11-06 21:34:58 +0000802#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Guido van Rossum2dced8b2007-10-30 17:27:30 +0000803 /* Set sys.stderr, replaces the preliminary stderr */
Christian Heimes58cb1b82007-11-13 02:19:40 +0000804 fd = fileno(stderr);
805 if (fd < 0) {
806#ifdef MS_WINDOWS
807 std = Py_None;
808 Py_INCREF(std);
809#else
810 goto error;
811#endif
812 }
813 else {
Guido van Rossume7fc50f2007-12-03 22:54:21 +0000814 if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
Christian Heimes58cb1b82007-11-13 02:19:40 +0000815 "\n", 0))) {
816 goto error;
817 }
818 } /* if (fd < 0) */
Christian Heimesdb233082007-11-13 02:34:21 +0000819 PySys_SetObject("__stderr__", std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000820 PySys_SetObject("stderr", std);
821 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000822#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000823
Christian Heimes58cb1b82007-11-13 02:19:40 +0000824 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000825 error:
Christian Heimesdb233082007-11-13 02:34:21 +0000826 status = -1;
827 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +0000828
829 Py_XDECREF(bimod);
830 Py_XDECREF(iomod);
831 return status;
832}
833
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000834/* Parse input from a file and execute it */
835
836int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000837PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000838 PyCompilerFlags *flags)
839{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 if (filename == NULL)
841 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000842 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000843 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000844 if (closeit)
845 fclose(fp);
846 return err;
847 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000848 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000849 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000850}
851
852int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000853PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000854{
Guido van Rossum82598051997-03-05 00:20:32 +0000855 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000856 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000857 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000858
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000859 if (flags == NULL) {
860 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000861 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000862 }
Guido van Rossum82598051997-03-05 00:20:32 +0000863 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000864 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000865 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000866 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000867 }
Guido van Rossum82598051997-03-05 00:20:32 +0000868 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000869 if (v == NULL) {
Neal Norwitza369c5a2007-08-25 07:41:59 +0000870 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000871 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000872 }
873 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000874 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000875 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000876 if (ret == E_EOF)
877 return 0;
878 /*
879 if (ret == E_NOMEM)
880 return -1;
881 */
882 }
883}
884
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000885/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000886#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000887 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Guido van Rossum45aecf42006-03-15 04:58:47 +0000888 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000889
Thomas Wouters89f507f2006-12-13 04:49:30 +0000890#if 0
891/* Keep an example of flags with future keyword support. */
892#define PARSER_FLAGS(flags) \
893 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
894 PyPARSE_DONT_IMPLY_DEDENT : 0) \
895 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
896 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
897#endif
898
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000899int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000900PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000901{
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000902 PyObject *m, *d, *v, *w, *oenc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000904 PyArena *arena;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000905 char *ps1 = "", *ps2 = "", *enc = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000907
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000908 if (fp == stdin) {
909 /* Fetch encoding from sys.stdin */
910 v = PySys_GetObject("stdin");
Christian Heimes2be03732007-11-15 02:26:46 +0000911 if (v == NULL || v == Py_None)
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000912 return -1;
913 oenc = PyObject_GetAttrString(v, "encoding");
914 if (!oenc)
915 return -1;
916 enc = PyUnicode_AsString(oenc);
917 }
Guido van Rossum82598051997-03-05 00:20:32 +0000918 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000919 if (v != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000920 v = PyObject_Str(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000921 if (v == NULL)
922 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000923 else if (PyUnicode_Check(v))
924 ps1 = PyUnicode_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000925 }
Guido van Rossum82598051997-03-05 00:20:32 +0000926 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000927 if (w != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +0000928 w = PyObject_Str(w);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000929 if (w == NULL)
930 PyErr_Clear();
Guido van Rossumc5724de2007-10-10 21:38:59 +0000931 else if (PyUnicode_Check(w))
932 ps2 = PyUnicode_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000933 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000934 arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000935 if (arena == NULL) {
936 Py_XDECREF(v);
937 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000938 Py_XDECREF(oenc);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000939 return -1;
940 }
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000941 mod = PyParser_ASTFromFile(fp, filename, enc,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000943 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000944 Py_XDECREF(v);
945 Py_XDECREF(w);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000946 Py_XDECREF(oenc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000948 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949 if (errcode == E_EOF) {
950 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000951 return E_EOF;
952 }
Guido van Rossum82598051997-03-05 00:20:32 +0000953 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000955 }
Guido van Rossum82598051997-03-05 00:20:32 +0000956 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000957 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000958 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000959 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000960 }
Guido van Rossum82598051997-03-05 00:20:32 +0000961 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000962 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000963 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000964 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000965 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000966 return -1;
967 }
Guido van Rossum82598051997-03-05 00:20:32 +0000968 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000969 return 0;
970}
971
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000972/* Check whether a file maybe a pyc file: Look at the extension,
973 the file type, and, if we may close it, at the first few bytes. */
974
975static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000976maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000977{
978 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
979 return 1;
980
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000981 /* Only look into the file if we are allowed to close it, since
982 it then should also be seekable. */
983 if (closeit) {
984 /* Read only two bytes of the magic. If the file was opened in
985 text mode, the bytes 3 and 4 of the magic (\r\n) might not
986 be read as they are on disk. */
987 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
988 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000989 /* Mess: In case of -x, the stream is NOT at its start now,
990 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000991 which makes the current stream position formally undefined,
992 and a x-platform nightmare.
993 Unfortunately, we have no direct way to know whether -x
994 was specified. So we use a terrible hack: if the current
995 stream position is not 0, we assume -x was specified, and
996 give up. Bug 132850 on SourceForge spells out the
997 hopelessness of trying anything else (fseek and ftell
998 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000999 */
Tim Peters3e876562001-02-11 04:35:39 +00001000 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001001 if (ftell(fp) == 0) {
1002 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +00001003 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +00001004 ispyc = 1;
1005 rewind(fp);
1006 }
Tim Peters3e876562001-02-11 04:35:39 +00001007 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001008 }
1009 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001010}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001011
Guido van Rossum0df002c2000-08-27 19:21:52 +00001012int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001013PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001014 PyCompilerFlags *flags)
1015{
Guido van Rossum82598051997-03-05 00:20:32 +00001016 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001017 const char *ext;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001018 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001019
Guido van Rossum82598051997-03-05 00:20:32 +00001020 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001021 if (m == NULL)
1022 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001023 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +00001024 if (PyDict_GetItemString(d, "__file__") == NULL) {
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001025 PyObject *f;
1026 f = PyUnicode_DecodeFSDefault(filename);
Fred Drake8ed02042002-10-17 21:24:58 +00001027 if (f == NULL)
1028 return -1;
1029 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1030 Py_DECREF(f);
1031 return -1;
1032 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001033 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +00001034 Py_DECREF(f);
1035 }
Guido van Rossumfdef2711994-09-14 13:31:04 +00001036 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001037 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001038 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +00001039 if (closeit)
1040 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +00001041 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +00001042 fprintf(stderr, "python: Can't reopen .pyc file\n");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001043 ret = -1;
1044 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001045 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +00001046 /* Turn on optimization if a .pyo file is given */
1047 if (strcmp(ext, ".pyo") == 0)
1048 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001049 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001050 } else {
Tim Petersd08e3822003-04-17 15:24:21 +00001051 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001052 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001053 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001054 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001055 PyErr_Print();
Guido van Rossumd8faa362007-04-27 19:54:29 +00001056 ret = -1;
1057 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001058 }
Guido van Rossum82598051997-03-05 00:20:32 +00001059 Py_DECREF(v);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001060 ret = 0;
1061 done:
1062 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1063 PyErr_Clear();
1064 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001065}
1066
1067int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001068PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001069{
Guido van Rossum82598051997-03-05 00:20:32 +00001070 PyObject *m, *d, *v;
1071 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001072 if (m == NULL)
1073 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +00001074 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +00001075 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001076 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +00001077 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001078 return -1;
1079 }
Guido van Rossum82598051997-03-05 00:20:32 +00001080 Py_DECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001081 return 0;
1082}
1083
Barry Warsaw035574d1997-08-29 22:07:17 +00001084static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001085parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1086 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001087{
1088 long hold;
1089 PyObject *v;
1090
1091 /* old style errors */
1092 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +00001093 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001094 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001095
1096 /* new style errors. `err' is an instance */
1097
1098 if (! (v = PyObject_GetAttrString(err, "msg")))
1099 goto finally;
1100 *message = v;
1101
1102 if (!(v = PyObject_GetAttrString(err, "filename")))
1103 goto finally;
1104 if (v == Py_None)
1105 *filename = NULL;
1106 else if (! (*filename = PyString_AsString(v)))
1107 goto finally;
1108
1109 Py_DECREF(v);
1110 if (!(v = PyObject_GetAttrString(err, "lineno")))
1111 goto finally;
Christian Heimes217cfd12007-12-02 14:31:20 +00001112 hold = PyLong_AsLong(v);
Barry Warsaw035574d1997-08-29 22:07:17 +00001113 Py_DECREF(v);
1114 v = NULL;
1115 if (hold < 0 && PyErr_Occurred())
1116 goto finally;
1117 *lineno = (int)hold;
1118
1119 if (!(v = PyObject_GetAttrString(err, "offset")))
1120 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001121 if (v == Py_None) {
1122 *offset = -1;
1123 Py_DECREF(v);
1124 v = NULL;
1125 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +00001126 hold = PyLong_AsLong(v);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001127 Py_DECREF(v);
1128 v = NULL;
1129 if (hold < 0 && PyErr_Occurred())
1130 goto finally;
1131 *offset = (int)hold;
1132 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001133
1134 if (!(v = PyObject_GetAttrString(err, "text")))
1135 goto finally;
1136 if (v == Py_None)
1137 *text = NULL;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001138 else if (!PyUnicode_Check(v) ||
1139 !(*text = PyUnicode_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001140 goto finally;
1141 Py_DECREF(v);
1142 return 1;
1143
1144finally:
1145 Py_XDECREF(v);
1146 return 0;
1147}
1148
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001149void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001150PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001151{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001152 PyErr_PrintEx(1);
1153}
1154
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001155static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001156print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001157{
1158 char *nl;
1159 if (offset >= 0) {
1160 if (offset > 0 && offset == (int)strlen(text))
1161 offset--;
1162 for (;;) {
1163 nl = strchr(text, '\n');
1164 if (nl == NULL || nl-text >= offset)
1165 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001166 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001167 text = nl+1;
1168 }
1169 while (*text == ' ' || *text == '\t') {
1170 text++;
1171 offset--;
1172 }
1173 }
1174 PyFile_WriteString(" ", f);
1175 PyFile_WriteString(text, f);
1176 if (*text == '\0' || text[strlen(text)-1] != '\n')
1177 PyFile_WriteString("\n", f);
1178 if (offset == -1)
1179 return;
1180 PyFile_WriteString(" ", f);
1181 offset--;
1182 while (offset > 0) {
1183 PyFile_WriteString(" ", f);
1184 offset--;
1185 }
1186 PyFile_WriteString("^\n", f);
1187}
1188
Guido van Rossum66e8e862001-03-23 17:54:43 +00001189static void
1190handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001191{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001192 PyObject *exception, *value, *tb;
1193 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001194
Guido van Rossumd8faa362007-04-27 19:54:29 +00001195 if (Py_InspectFlag)
1196 /* Don't exit if -i flag was given. This flag is set to 0
1197 * when entering interactive mode for inspecting. */
1198 return;
1199
Guido van Rossum66e8e862001-03-23 17:54:43 +00001200 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001201 fflush(stdout);
1202 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001203 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001204 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001205 /* The error code should be in the `code' attribute. */
1206 PyObject *code = PyObject_GetAttrString(value, "code");
1207 if (code) {
1208 Py_DECREF(value);
1209 value = code;
1210 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001211 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001212 }
1213 /* If we failed to dig out the 'code' attribute,
1214 just let the else clause below print the error. */
1215 }
Christian Heimes217cfd12007-12-02 14:31:20 +00001216 if (PyLong_Check(value))
1217 exitcode = (int)PyLong_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001218 else {
1219 PyObject_Print(value, stderr, Py_PRINT_RAW);
1220 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001221 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001222 }
Tim Peterscf615b52003-04-19 18:47:02 +00001223 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001224 /* Restore and clear the exception info, in order to properly decref
1225 * the exception, value, and traceback. If we just exit instead,
1226 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1227 * some finalizers from running.
1228 */
Tim Peterscf615b52003-04-19 18:47:02 +00001229 PyErr_Restore(exception, value, tb);
1230 PyErr_Clear();
1231 Py_Exit(exitcode);
1232 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001233}
1234
1235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001236PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001237{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001238 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001239
1240 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1241 handle_system_exit();
1242 }
Guido van Rossum82598051997-03-05 00:20:32 +00001243 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (exception == NULL)
1245 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001246 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001247 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001248 return;
Guido van Rossum9d785502006-03-07 18:31:44 +00001249 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001250 if (set_sys_last_vars) {
1251 PySys_SetObject("last_type", exception);
1252 PySys_SetObject("last_value", v);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001253 PySys_SetObject("last_traceback", tb ? tb : Py_None);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001254 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001255 hook = PySys_GetObject("excepthook");
1256 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001257 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001258 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001259 PyObject *result = PyEval_CallObject(hook, args);
1260 if (result == NULL) {
1261 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001262 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1263 handle_system_exit();
1264 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001265 PyErr_Fetch(&exception2, &v2, &tb2);
1266 PyErr_NormalizeException(&exception2, &v2, &tb2);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001267 /* It should not be possible for exception2 or v2
1268 to be NULL. However PyErr_Display() can't
1269 tolerate NULLs, so just be safe. */
1270 if (exception2 == NULL) {
1271 exception2 = Py_None;
1272 Py_INCREF(exception2);
1273 }
1274 if (v2 == NULL) {
1275 v2 = Py_None;
1276 Py_INCREF(v2);
1277 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001278 fflush(stdout);
1279 PySys_WriteStderr("Error in sys.excepthook:\n");
1280 PyErr_Display(exception2, v2, tb2);
1281 PySys_WriteStderr("\nOriginal exception was:\n");
1282 PyErr_Display(exception, v, tb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001283 Py_DECREF(exception2);
1284 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001285 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001286 }
1287 Py_XDECREF(result);
1288 Py_XDECREF(args);
1289 } else {
1290 PySys_WriteStderr("sys.excepthook is missing\n");
1291 PyErr_Display(exception, v, tb);
1292 }
1293 Py_XDECREF(exception);
1294 Py_XDECREF(v);
1295 Py_XDECREF(tb);
1296}
1297
Thomas Wouters477c8d52006-05-27 19:21:47 +00001298void
1299PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001300{
1301 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001302 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001303 Py_INCREF(value);
Christian Heimes2be03732007-11-15 02:26:46 +00001304 if (f == Py_None) {
1305 /* pass */
1306 }
1307 else if (f == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001308 _PyObject_Dump(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001309 fprintf(stderr, "lost sys.stderr\n");
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001310 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001311 else {
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001312 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001313 if (tb && tb != Py_None)
1314 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001315 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001316 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001317 {
Guido van Rossum82598051997-03-05 00:20:32 +00001318 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001319 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001320 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001321 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001322 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001323 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001324 else {
1325 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001326 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001327 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001328 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001329 else
Guido van Rossum82598051997-03-05 00:20:32 +00001330 PyFile_WriteString(filename, f);
1331 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001332 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001333 PyFile_WriteString(buf, f);
1334 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001335 if (text != NULL)
1336 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001337 Py_DECREF(value);
1338 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001339 /* Can't be bothered to check all those
1340 PyFile_WriteString() calls */
1341 if (PyErr_Occurred())
1342 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001343 }
1344 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001345 if (err) {
1346 /* Don't do anything else */
1347 }
Brett Cannonbf364092006-03-01 04:25:17 +00001348 else if (PyExceptionClass_Check(exception)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349 PyObject* moduleName;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001350 char* className = PyExceptionClass_Name(exception);
1351 if (className != NULL) {
1352 char *dot = strrchr(className, '.');
1353 if (dot != NULL)
1354 className = dot+1;
1355 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001356
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001357 moduleName = PyObject_GetAttrString(exception, "__module__");
Guido van Rossumc5724de2007-10-10 21:38:59 +00001358 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1359 {
1360 Py_DECREF(moduleName);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001361 err = PyFile_WriteString("<unknown>", f);
Guido van Rossumc5724de2007-10-10 21:38:59 +00001362 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001363 else {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001364 char* modstr = PyUnicode_AsString(moduleName);
Georg Brandl1a3284e2007-12-02 09:40:06 +00001365 if (modstr && strcmp(modstr, "builtins"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001366 {
1367 err = PyFile_WriteString(modstr, f);
1368 err += PyFile_WriteString(".", f);
1369 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001370 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001371 }
1372 if (err == 0) {
1373 if (className == NULL)
1374 err = PyFile_WriteString("<unknown>", f);
1375 else
Brett Cannonbf364092006-03-01 04:25:17 +00001376 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001377 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001378 }
1379 else
1380 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001381 if (err == 0 && (value != Py_None)) {
Thomas Heller519a0422007-11-15 20:48:54 +00001382 PyObject *s = PyObject_Str(value);
Brett Cannon2e63b732006-03-02 18:34:57 +00001383 /* only print colon if the str() of the
1384 object is not the empty string
1385 */
1386 if (s == NULL)
1387 err = -1;
Guido van Rossumc5724de2007-10-10 21:38:59 +00001388 else if (!PyUnicode_Check(s) ||
1389 PyUnicode_GetSize(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001390 err = PyFile_WriteString(": ", f);
1391 if (err == 0)
1392 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1393 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001394 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001395 /* try to write a newline in any case */
1396 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001397 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001398 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001399 /* If an error happened here, don't show it.
1400 XXX This is wrong, but too many callers rely on this behavior. */
1401 if (err != 0)
1402 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001403}
1404
Guido van Rossum82598051997-03-05 00:20:32 +00001405PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001406PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001408{
Neal Norwitze92fba02006-03-04 18:52:26 +00001409 PyObject *ret = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001410 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001411 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001412 if (arena == NULL)
1413 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001414
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001415 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001416 if (mod != NULL)
1417 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001418 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001420}
1421
1422PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001423PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001424 PyObject *locals, int closeit, PyCompilerFlags *flags)
1425{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 PyObject *ret;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001427 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001428 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001429 if (arena == NULL)
1430 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001431
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001432 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001433 flags, NULL, arena);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001434 if (closeit)
1435 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001436 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001437 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001439 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001440 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001441 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001443}
1444
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001445static void
1446flush_io(void)
1447{
1448 PyObject *f, *r;
1449 f = PySys_GetObject("stderr");
1450 if (f != NULL) {
1451 r = PyObject_CallMethod(f, "flush", "");
1452 if (r)
1453 Py_DECREF(r);
1454 else
1455 PyErr_Clear();
1456 }
1457 f = PySys_GetObject("stdout");
1458 if (f != NULL) {
1459 r = PyObject_CallMethod(f, "flush", "");
1460 if (r)
1461 Py_DECREF(r);
1462 else
1463 PyErr_Clear();
1464 }
1465}
1466
Guido van Rossum82598051997-03-05 00:20:32 +00001467static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001470{
Guido van Rossum82598051997-03-05 00:20:32 +00001471 PyCodeObject *co;
1472 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001473 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001474 if (co == NULL)
1475 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001476 v = PyEval_EvalCode(co, globals, locals);
1477 Py_DECREF(co);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001478 flush_io();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001479 return v;
1480}
1481
Guido van Rossum82598051997-03-05 00:20:32 +00001482static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001483run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001485{
Guido van Rossum82598051997-03-05 00:20:32 +00001486 PyCodeObject *co;
1487 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001488 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001489 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001490
Guido van Rossum82598051997-03-05 00:20:32 +00001491 magic = PyMarshal_ReadLongFromFile(fp);
1492 if (magic != PyImport_GetMagicNumber()) {
1493 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001494 "Bad magic number in .pyc file");
1495 return NULL;
1496 }
Guido van Rossum82598051997-03-05 00:20:32 +00001497 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001498 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001499 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001500 if (v == NULL || !PyCode_Check(v)) {
1501 Py_XDECREF(v);
1502 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001503 "Bad code object in .pyc file");
1504 return NULL;
1505 }
Guido van Rossum82598051997-03-05 00:20:32 +00001506 co = (PyCodeObject *)v;
1507 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001508 if (v && flags)
1509 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001510 Py_DECREF(co);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00001511 flush_io();
Guido van Rossumfdef2711994-09-14 13:31:04 +00001512 return v;
1513}
1514
Guido van Rossum82598051997-03-05 00:20:32 +00001515PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001516Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001517 PyCompilerFlags *flags)
1518{
Guido van Rossum82598051997-03-05 00:20:32 +00001519 PyCodeObject *co;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001520 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001521 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001522 if (arena == NULL)
1523 return NULL;
1524
1525 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001526 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001527 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001528 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001529 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001530 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001531 PyObject *result = PyAST_mod2obj(mod);
1532 PyArena_Free(arena);
1533 return result;
1534 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001535 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001536 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001537 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001538}
1539
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001540struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001541Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001542{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001543 struct symtable *st;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001544 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001545 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001546 if (arena == NULL)
1547 return NULL;
1548
1549 mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001550 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001551 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001552 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001555 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001556 return st;
1557}
1558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559/* Preferred access to parser is through AST. */
1560mod_ty
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001561PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001562 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 mod_ty mod;
1565 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001566 node *n = PyParser_ParseStringFlagsFilename(s, filename,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001567 &_PyParser_Grammar, start, &err,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 PARSER_FLAGS(flags));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 if (n) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001570 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 PyNode_Free(n);
1572 return mod;
1573 }
1574 else {
1575 err_input(&err);
1576 return NULL;
1577 }
1578}
1579
1580mod_ty
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001581PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1582 int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001583 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001584 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 mod_ty mod;
1587 perrdetail err;
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001588 node *n = PyParser_ParseFileFlags(fp, filename, enc,
1589 &_PyParser_Grammar,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001590 start, ps1, ps2, &err, PARSER_FLAGS(flags));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 if (n) {
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001592 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 PyNode_Free(n);
1594 return mod;
1595 }
1596 else {
1597 err_input(&err);
1598 if (errcode)
1599 *errcode = err.error;
1600 return NULL;
1601 }
1602}
1603
Guido van Rossuma110aa61994-08-29 12:50:44 +00001604/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001605
Guido van Rossuma110aa61994-08-29 12:50:44 +00001606node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001607PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001608{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001609 perrdetail err;
Martin v. Löwis85bcc662007-09-04 09:18:06 +00001610 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1611 &_PyParser_Grammar,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001612 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001613 if (n == NULL)
1614 err_input(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001615
Guido van Rossuma110aa61994-08-29 12:50:44 +00001616 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001617}
1618
Guido van Rossuma110aa61994-08-29 12:50:44 +00001619/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001620
Guido van Rossuma110aa61994-08-29 12:50:44 +00001621node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001622PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001623{
Tim Petersfe2127d2001-07-16 05:37:24 +00001624 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001625 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1626 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001627 if (n == NULL)
1628 err_input(&err);
1629 return n;
1630}
1631
1632node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001633PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001634 int start, int flags)
1635{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001636 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001637 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1638 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001639 if (n == NULL)
1640 err_input(&err);
1641 return n;
1642}
1643
1644node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001645PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001646{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001647 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001648}
1649
Guido van Rossum66ebd912003-04-17 16:02:26 +00001650/* May want to move a more generalized form of this to parsetok.c or
1651 even parser modules. */
1652
1653void
1654PyParser_SetError(perrdetail *err)
1655{
1656 err_input(err);
1657}
1658
Guido van Rossuma110aa61994-08-29 12:50:44 +00001659/* Set the error appropriate to the given input error code (see errcode.h) */
1660
1661static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001662err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001663{
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001664 PyObject *v, *w, *errtype, *errtext;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001665 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001666 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001667 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001668 switch (err->error) {
1669 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001670 errtype = PyExc_IndentationError;
1671 if (err->expected == INDENT)
1672 msg = "expected an indented block";
1673 else if (err->token == INDENT)
1674 msg = "unexpected indent";
1675 else if (err->token == DEDENT)
1676 msg = "unexpected unindent";
1677 else {
1678 errtype = PyExc_SyntaxError;
1679 msg = "invalid syntax";
1680 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001681 break;
1682 case E_TOKEN:
1683 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001684 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001685 case E_EOFS:
1686 msg = "EOF while scanning triple-quoted string";
1687 break;
1688 case E_EOLS:
1689 msg = "EOL while scanning single-quoted string";
1690 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001691 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001692 if (!PyErr_Occurred())
1693 PyErr_SetNone(PyExc_KeyboardInterrupt);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001694 return;
1695 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001696 PyErr_NoMemory();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001697 return;
1698 case E_EOF:
1699 msg = "unexpected EOF while parsing";
1700 break;
Fred Drake85f36392000-07-11 17:53:00 +00001701 case E_TABSPACE:
1702 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001703 msg = "inconsistent use of tabs and spaces in indentation";
1704 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001705 case E_OVERFLOW:
1706 msg = "expression too long";
1707 break;
Fred Drake85f36392000-07-11 17:53:00 +00001708 case E_DEDENT:
1709 errtype = PyExc_IndentationError;
1710 msg = "unindent does not match any outer indentation level";
1711 break;
1712 case E_TOODEEP:
1713 errtype = PyExc_IndentationError;
1714 msg = "too many levels of indentation";
1715 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001716 case E_DECODE: {
1717 PyObject *type, *value, *tb;
1718 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001719 if (value != NULL) {
Thomas Heller519a0422007-11-15 20:48:54 +00001720 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001721 if (u != NULL) {
Guido van Rossumc5724de2007-10-10 21:38:59 +00001722 msg = PyUnicode_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001723 }
1724 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001725 if (msg == NULL)
1726 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001727 Py_XDECREF(type);
1728 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001729 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001730 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001731 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001732 case E_LINECONT:
1733 msg = "unexpected character after line continuation character";
1734 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00001735
1736 case E_IDENTIFIER:
1737 msg = "invalid character in identifier";
1738 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001739 default:
1740 fprintf(stderr, "error=%d\n", err->error);
1741 msg = "unknown parsing error";
1742 break;
1743 }
Martin v. Löwis5deb2102007-08-31 11:17:42 +00001744 /* err->text may not be UTF-8 in case of decoding errors.
1745 Explicitly convert to an object. */
1746 if (!err->text) {
1747 errtext = Py_None;
1748 Py_INCREF(Py_None);
1749 } else {
1750 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1751 "replace");
1752 }
1753 v = Py_BuildValue("(ziiN)", err->filename,
1754 err->lineno, err->offset, errtext);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001755 if (err->text != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756 PyObject_FREE(err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 err->text = NULL;
1758 }
1759 w = NULL;
1760 if (v != NULL)
1761 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001762 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001763 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001764 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001765 Py_XDECREF(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001766}
1767
1768/* Print fatal error message and abort */
1769
1770void
Tim Peters7c321a82002-07-09 02:57:01 +00001771Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001772{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001773 fprintf(stderr, "Fatal Python error: %s\n", msg);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001774 if (PyErr_Occurred()) {
1775 PyErr_Print();
1776 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001777#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001778 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001779 OutputDebugString(msg);
1780 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001781#ifdef _DEBUG
1782 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001783#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001784#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001785 abort();
1786}
1787
1788/* Clean up and exit */
1789
Guido van Rossuma110aa61994-08-29 12:50:44 +00001790#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001791#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001792#endif
1793
Collin Winter670e6922007-03-21 02:57:17 +00001794static void (*pyexitfunc)(void) = NULL;
1795/* For the atexit module. */
1796void _Py_PyAtExit(void (*func)(void))
1797{
1798 pyexitfunc = func;
1799}
1800
1801static void
1802call_py_exitfuncs(void)
1803{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001804 if (pyexitfunc == NULL)
Collin Winter670e6922007-03-21 02:57:17 +00001805 return;
1806
1807 (*pyexitfunc)();
1808 PyErr_Clear();
1809}
1810
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001811#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001812static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001813static int nexitfuncs = 0;
1814
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001815int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001816{
1817 if (nexitfuncs >= NEXITFUNCS)
1818 return -1;
1819 exitfuncs[nexitfuncs++] = func;
1820 return 0;
1821}
1822
Guido van Rossumcc283f51997-08-05 02:22:03 +00001823static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001824call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001825{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001826 while (nexitfuncs > 0)
1827 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001828
1829 fflush(stdout);
1830 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001831}
1832
1833void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001834Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001835{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001836 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001837
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001838 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001839}
1840
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001841static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001842initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001843{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001844#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001845 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001846#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001847#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001848 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001849#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001850#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001851 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001852#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001853 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001854}
1855
Guido van Rossum7433b121997-02-14 19:45:36 +00001856
1857/*
1858 * The file descriptor fd is considered ``interactive'' if either
1859 * a) isatty(fd) is TRUE, or
1860 * b) the -i flag was given, and the filename associated with
1861 * the descriptor is NULL or "<stdin>" or "???".
1862 */
1863int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001864Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001865{
1866 if (isatty((int)fileno(fp)))
1867 return 1;
1868 if (!Py_InteractiveFlag)
1869 return 0;
1870 return (filename == NULL) ||
1871 (strcmp(filename, "<stdin>") == 0) ||
1872 (strcmp(filename, "???") == 0);
1873}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001874
1875
Tim Petersd08e3822003-04-17 15:24:21 +00001876#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001877#if defined(WIN32) && defined(_MSC_VER)
1878
1879/* Stack checking for Microsoft C */
1880
1881#include <malloc.h>
1882#include <excpt.h>
1883
Fred Drakee8de31c2000-08-31 05:38:39 +00001884/*
1885 * Return non-zero when we run out of memory on the stack; zero otherwise.
1886 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001887int
Fred Drake399739f2000-08-31 05:52:44 +00001888PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001889{
1890 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001891 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001892 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001893 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001894 return 0;
1895 } __except (EXCEPTION_EXECUTE_HANDLER) {
1896 /* just ignore all errors */
1897 }
1898 return 1;
1899}
1900
1901#endif /* WIN32 && _MSC_VER */
1902
1903/* Alternate implementations can be added here... */
1904
1905#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001906
1907
1908/* Wrappers around sigaction() or signal(). */
1909
1910PyOS_sighandler_t
1911PyOS_getsig(int sig)
1912{
1913#ifdef HAVE_SIGACTION
1914 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001915 if (sigaction(sig, NULL, &context) == -1)
1916 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001917 return context.sa_handler;
1918#else
1919 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001920/* Special signal handling for the secure CRT in Visual Studio 2005 */
1921#if defined(_MSC_VER) && _MSC_VER >= 1400
1922 switch (sig) {
1923 /* Only these signals are valid */
1924 case SIGINT:
1925 case SIGILL:
1926 case SIGFPE:
1927 case SIGSEGV:
1928 case SIGTERM:
1929 case SIGBREAK:
1930 case SIGABRT:
1931 break;
1932 /* Don't call signal() with other values or it will assert */
1933 default:
1934 return SIG_ERR;
1935 }
1936#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001937 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001938 if (handler != SIG_ERR)
1939 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001940 return handler;
1941#endif
1942}
1943
1944PyOS_sighandler_t
1945PyOS_setsig(int sig, PyOS_sighandler_t handler)
1946{
1947#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001948 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001949 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001950 sigemptyset(&context.sa_mask);
1951 context.sa_flags = 0;
1952 if (sigaction(sig, &context, &ocontext) == -1)
1953 return SIG_ERR;
1954 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001955#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001956 PyOS_sighandler_t oldhandler;
1957 oldhandler = signal(sig, handler);
1958#ifdef HAVE_SIGINTERRUPT
1959 siginterrupt(sig, 1);
1960#endif
1961 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001962#endif
1963}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964
1965/* Deprecated C API functions still provided for binary compatiblity */
1966
1967#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1970{
1971 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1972}
1973
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001974#undef PyParser_SimpleParseString
1975PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976PyParser_SimpleParseString(const char *str, int start)
1977{
1978 return PyParser_SimpleParseStringFlags(str, start, 0);
1979}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001980
1981#undef PyRun_AnyFile
1982PyAPI_FUNC(int)
1983PyRun_AnyFile(FILE *fp, const char *name)
1984{
1985 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1986}
1987
1988#undef PyRun_AnyFileEx
1989PyAPI_FUNC(int)
1990PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1991{
1992 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1993}
1994
1995#undef PyRun_AnyFileFlags
1996PyAPI_FUNC(int)
1997PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1998{
1999 return PyRun_AnyFileExFlags(fp, name, 0, flags);
2000}
2001
2002#undef PyRun_File
2003PyAPI_FUNC(PyObject *)
2004PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2005{
2006 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2007}
2008
2009#undef PyRun_FileEx
2010PyAPI_FUNC(PyObject *)
2011PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2012{
2013 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2014}
2015
2016#undef PyRun_FileFlags
2017PyAPI_FUNC(PyObject *)
2018PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2019 PyCompilerFlags *flags)
2020{
2021 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2022}
2023
2024#undef PyRun_SimpleFile
2025PyAPI_FUNC(int)
2026PyRun_SimpleFile(FILE *f, const char *p)
2027{
2028 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2029}
2030
2031#undef PyRun_SimpleFileEx
2032PyAPI_FUNC(int)
2033PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2034{
2035 return PyRun_SimpleFileExFlags(f, p, c, NULL);
2036}
2037
2038
2039#undef PyRun_String
2040PyAPI_FUNC(PyObject *)
2041PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2042{
2043 return PyRun_StringFlags(str, s, g, l, NULL);
2044}
2045
2046#undef PyRun_SimpleString
2047PyAPI_FUNC(int)
2048PyRun_SimpleString(const char *s)
2049{
2050 return PyRun_SimpleStringFlags(s, NULL);
2051}
2052
2053#undef Py_CompileString
2054PyAPI_FUNC(PyObject *)
2055Py_CompileString(const char *str, const char *p, int s)
2056{
2057 return Py_CompileStringFlags(str, p, s, NULL);
2058}
2059
2060#undef PyRun_InteractiveOne
2061PyAPI_FUNC(int)
2062PyRun_InteractiveOne(FILE *f, const char *p)
2063{
2064 return PyRun_InteractiveOneFlags(f, p, NULL);
2065}
2066
2067#undef PyRun_InteractiveLoop
2068PyAPI_FUNC(int)
2069PyRun_InteractiveLoop(FILE *f, const char *p)
2070{
2071 return PyRun_InteractiveLoopFlags(f, p, NULL);
2072}
2073
2074#ifdef __cplusplus
2075}
2076#endif