blob: 54f3c5784bd86b1384e07ad4214890a971111a2d [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000020
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000021#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000022#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000023#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000024
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
Tim Peters62e97f02006-03-28 21:44:32 +000036#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000037#else /* Py_REF_DEBUG */
Tim Peters62e97f02006-03-28 21:44:32 +000038#define PRINT_TOTAL_REFS() fprintf(stderr, \
39 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
Armin Rigoe1709372006-04-12 17:06:05 +000040 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000041#endif
42
Anthony Baxterac6bd462006-04-13 02:06:09 +000043#ifdef __cplusplus
44extern "C" {
45#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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Neal Norwitzadb69fc2005-12-17 20:54:49 +000055 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000056static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Jeremy Hyltonbc320242001-03-22 02:47:58 +000057 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000058static void err_input(perrdetail *);
59static void initsigs(void);
60static void call_sys_exitfunc(void);
61static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000062extern void _PyUnicode_Init(void);
63extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000064
Mark Hammond8d98d2c2003-04-19 15:41:53 +000065#ifdef WITH_THREAD
66extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
67extern void _PyGILState_Fini(void);
68#endif /* WITH_THREAD */
69
Guido van Rossum82598051997-03-05 00:20:32 +000070int Py_DebugFlag; /* Needed by parser.c */
71int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000072int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl49aafc92007-03-07 00:34:46 +000073int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000074int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000075int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000076int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000077int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000078int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000079int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000080int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000081/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
83 true divisions (which they will be in 2.3). */
84int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000085int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000086
Brett Cannone9746892008-04-12 23:44:07 +000087/* PyModule_GetWarningsModule is no longer necessary as of 2.6
88since _warnings is builtin. This API should not be used. */
89PyObject *
90PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +000091{
Brett Cannone9746892008-04-12 23:44:07 +000092 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +000093}
Mark Hammonda43fd0c2003-02-19 00:33:33 +000094
Guido van Rossum25ce5661997-08-02 03:10:38 +000095static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +000096
Thomas Wouters7e474022000-07-16 12:04:32 +000097/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +000098
99int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000100Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000101{
102 return initialized;
103}
104
Guido van Rossum25ce5661997-08-02 03:10:38 +0000105/* Global initializations. Can be undone by Py_Finalize(). Don't
106 call this twice without an intervening Py_Finalize() call. When
107 initializations fail, a fatal error is issued and the function does
108 not return. On return, the first thread and interpreter state have
109 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000110
Guido van Rossum25ce5661997-08-02 03:10:38 +0000111 Locking: you must hold the interpreter lock while calling this.
112 (If the lock has not yet been initialized, that's equivalent to
113 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000114
Guido van Rossum25ce5661997-08-02 03:10:38 +0000115*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000116
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000117static int
118add_flag(int flag, const char *envs)
119{
120 int env = atoi(envs);
121 if (flag < env)
122 flag = env;
123 if (flag < 1)
124 flag = 1;
125 return flag;
126}
127
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000129Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000131 PyInterpreterState *interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000132 PyThreadState *tstate;
133 PyObject *bimod, *sysmod;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000134 char *p;
Neal Norwitz18aa3882008-08-24 05:04:52 +0000135 char *icodeset = NULL; /* On Windows, input codeset may theoretically
136 differ from output codeset. */
Martin v. Löwis99815892008-06-01 07:20:46 +0000137 char *codeset = NULL;
138 char *errors = NULL;
139 int free_codeset = 0;
140 int overridden = 0;
Martin v. Löwisb12d8572008-06-01 08:06:17 +0000141 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000142#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Martin v. Löwis99815892008-06-01 07:20:46 +0000143 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000144#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000145#ifdef MS_WINDOWS
146 char ibuf[128];
147 char buf[128];
148#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +0000149 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000150
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000151 if (initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000152 return;
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000153 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000154
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000155 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000156 Py_DebugFlag = add_flag(Py_DebugFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000157 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000158 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000159 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000160 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
Georg Brandl2da0fce2008-01-07 17:09:35 +0000161 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
162 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000163
Guido van Rossuma027efa1997-05-05 20:56:21 +0000164 interp = PyInterpreterState_New();
165 if (interp == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000166 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000167
Guido van Rossuma027efa1997-05-05 20:56:21 +0000168 tstate = PyThreadState_New(interp);
169 if (tstate == NULL)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000170 Py_FatalError("Py_Initialize: can't make first thread");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000171 (void) PyThreadState_Swap(tstate);
172
Guido van Rossum70d893a2001-08-16 08:21:42 +0000173 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000174
Neal Norwitzb2501f42002-12-31 03:42:13 +0000175 if (!_PyFrame_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000176 Py_FatalError("Py_Initialize: can't init frames");
177
Neal Norwitzb2501f42002-12-31 03:42:13 +0000178 if (!_PyInt_Init())
Neal Norwitzc91ed402002-12-30 22:29:22 +0000179 Py_FatalError("Py_Initialize: can't init ints");
180
Christian Heimes3497f942008-05-26 12:29:14 +0000181 if (!PyByteArray_Init())
Christian Heimes1a6387e2008-03-26 12:49:49 +0000182 Py_FatalError("Py_Initialize: can't init bytearray");
183
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000184 _PyFloat_Init();
185
Guido van Rossum25ce5661997-08-02 03:10:38 +0000186 interp->modules = PyDict_New();
187 if (interp->modules == NULL)
188 Py_FatalError("Py_Initialize: can't make modules dictionary");
Collin Winter276887b2007-03-12 16:11:39 +0000189 interp->modules_reloading = PyDict_New();
190 if (interp->modules_reloading == NULL)
191 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000192
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000193#ifdef Py_USING_UNICODE
Guido van Rossumc94044c2000-03-10 23:03:54 +0000194 /* Init Unicode implementation; relies on the codec registry */
195 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000196#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000197
Barry Warsawf242aa02000-05-25 23:09:49 +0000198 bimod = _PyBuiltin_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000199 if (bimod == NULL)
200 Py_FatalError("Py_Initialize: can't initialize __builtin__");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000201 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000202 if (interp->builtins == NULL)
203 Py_FatalError("Py_Initialize: can't initialize builtins dict");
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000204 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000205
206 sysmod = _PySys_Init();
207 if (sysmod == NULL)
208 Py_FatalError("Py_Initialize: can't initialize sys");
209 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0f7dbf72006-08-12 03:17:41 +0000210 if (interp->sysdict == NULL)
211 Py_FatalError("Py_Initialize: can't initialize sys dict");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000212 Py_INCREF(interp->sysdict);
213 _PyImport_FixupExtension("sys", "sys");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000214 PySys_SetPath(Py_GetPath());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000215 PyDict_SetItemString(interp->sysdict, "modules",
216 interp->modules);
217
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000218 _PyImport_Init();
219
Barry Warsawf242aa02000-05-25 23:09:49 +0000220 /* initialize builtin exceptions */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000221 _PyExc_Init();
Barry Warsaw5821bc52001-08-13 23:04:56 +0000222 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000223
Barry Warsaw035574d1997-08-29 22:07:17 +0000224 /* phase 2 of builtins */
Barry Warsaw963b8711997-09-18 16:42:02 +0000225 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000226
Just van Rossum52e14d62002-12-30 22:08:05 +0000227 _PyImportHooks_Init();
228
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000229 if (install_sigs)
230 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannone9746892008-04-12 23:44:07 +0000231
Georg Brandl3c0fd562008-07-05 10:07:18 +0000232 /* Initialize warnings. */
233 _PyWarnings_Init();
234 if (PySys_HasWarnOptions()) {
235 PyObject *warnings_module = PyImport_ImportModule("warnings");
236 if (!warnings_module)
237 PyErr_Clear();
238 Py_XDECREF(warnings_module);
239 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000240
241 initmain(); /* Module __main__ */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000242 if (!Py_NoSiteFlag)
243 initsite(); /* Module site */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000244
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000245 /* auto-thread-state API, if available */
246#ifdef WITH_THREAD
247 _PyGILState_Init(interp, tstate);
248#endif /* WITH_THREAD */
249
Martin v. Löwis99815892008-06-01 07:20:46 +0000250 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
251 p = icodeset = codeset = strdup(p);
252 free_codeset = 1;
253 errors = strchr(p, ':');
254 if (errors) {
255 *errors = '\0';
256 errors++;
257 }
258 overridden = 1;
259 }
260
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000261#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
262 /* On Unix, set the file system encoding according to the
263 user's preference, if the CODESET names a well-known
264 Python codec, and Py_FileSystemDefaultEncoding isn't
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000265 initialized by other means. Also set the encoding of
Martin v. Löwis99815892008-06-01 07:20:46 +0000266 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000267
Martin v. Löwis99815892008-06-01 07:20:46 +0000268 if (!overridden || !Py_FileSystemDefaultEncoding) {
269 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
270 setlocale(LC_CTYPE, "");
271 loc_codeset = nl_langinfo(CODESET);
272 if (loc_codeset && *loc_codeset) {
273 PyObject *enc = PyCodec_Encoder(loc_codeset);
274 if (enc) {
275 loc_codeset = strdup(loc_codeset);
276 Py_DECREF(enc);
277 } else {
278 loc_codeset = NULL;
279 PyErr_Clear();
280 }
281 } else
282 loc_codeset = NULL;
283 setlocale(LC_CTYPE, saved_locale);
284 free(saved_locale);
285
286 if (!overridden) {
287 codeset = icodeset = loc_codeset;
288 free_codeset = 1;
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000289 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000290
291 /* Initialize Py_FileSystemDefaultEncoding from
292 locale even if PYTHONIOENCODING is set. */
293 if (!Py_FileSystemDefaultEncoding) {
294 Py_FileSystemDefaultEncoding = loc_codeset;
295 if (!overridden)
296 free_codeset = 0;
297 }
298 }
299#endif
300
301#ifdef MS_WINDOWS
302 if (!overridden) {
303 icodeset = ibuf;
Martin v. Löwis6495c8d2008-06-01 08:19:02 +0000304 codeset = buf;
Martin v. Löwis99815892008-06-01 07:20:46 +0000305 sprintf(ibuf, "cp%d", GetConsoleCP());
306 sprintf(buf, "cp%d", GetConsoleOutputCP());
307 }
308#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000309
310 if (codeset) {
Michael W. Hudson68debc92003-08-11 12:20:24 +0000311 sys_stream = PySys_GetObject("stdin");
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000312 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
313 if (!sys_isatty)
314 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000315 if ((overridden ||
316 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000317 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000318 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000319 Py_FatalError("Cannot set codeset of stdin");
320 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000321 Py_XDECREF(sys_isatty);
322
323 sys_stream = PySys_GetObject("stdout");
324 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
325 if (!sys_isatty)
326 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000327 if ((overridden ||
328 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000329 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000330 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000331 Py_FatalError("Cannot set codeset of stdout");
332 }
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000333 Py_XDECREF(sys_isatty);
334
Martin v. Löwisea62d252006-04-03 10:56:49 +0000335 sys_stream = PySys_GetObject("stderr");
336 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
337 if (!sys_isatty)
338 PyErr_Clear();
Martin v. Löwis99815892008-06-01 07:20:46 +0000339 if((overridden ||
340 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
Thomas Woutersafea5292007-01-23 13:42:00 +0000341 PyFile_Check(sys_stream)) {
Martin v. Löwis99815892008-06-01 07:20:46 +0000342 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
Martin v. Löwisea62d252006-04-03 10:56:49 +0000343 Py_FatalError("Cannot set codeset of stderr");
344 }
345 Py_XDECREF(sys_isatty);
346
Martin v. Löwis99815892008-06-01 07:20:46 +0000347 if (free_codeset)
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000348 free(codeset);
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000349 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000350}
351
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000352void
353Py_Initialize(void)
354{
355 Py_InitializeEx(1);
356}
357
358
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000359#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000360extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000361#endif
362
Guido van Rossum25ce5661997-08-02 03:10:38 +0000363/* Undo the effect of Py_Initialize().
364
365 Beware: if multiple interpreter and/or thread states exist, these
366 are not wiped out; only the current thread and interpreter state
367 are deleted. But since everything else is deleted, those other
368 interpreter and thread states should no longer be used.
369
370 (XXX We should do better, e.g. wipe out all interpreters and
371 threads.)
372
373 Locking: as above.
374
375*/
376
377void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000378Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000379{
380 PyInterpreterState *interp;
381 PyThreadState *tstate;
382
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000383 if (!initialized)
Guido van Rossumaa615051997-08-20 22:40:18 +0000384 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000385
Tim Peters384fd102001-01-21 03:40:37 +0000386 /* The interpreter is still entirely intact at this point, and the
387 * exit funcs may be relying on that. In particular, if some thread
388 * or exit func is still waiting to do an import, the import machinery
389 * expects Py_IsInitialized() to return true. So don't say the
390 * interpreter is uninitialized until after the exit funcs have run.
391 * Note that Threading.py uses an exit func to do a join on all the
392 * threads created thru it, so this also protects pending imports in
393 * the threads created via Threading.
394 */
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000395 call_sys_exitfunc();
Tim Peters384fd102001-01-21 03:40:37 +0000396 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000397
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000398 /* Get current thread state and interpreter pointer */
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000399 tstate = PyThreadState_GET();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000400 interp = tstate->interp;
401
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000402 /* Disable signal handling */
403 PyOS_FiniInterrupts();
404
Christian Heimes908caac2008-01-27 23:34:59 +0000405 /* Clear type lookup cache */
406 PyType_ClearCache();
407
Guido van Rossume13ddc92003-04-17 17:29:22 +0000408 /* Collect garbage. This may call finalizers; it's nice to call these
Tim Peters1d7323e2003-12-01 21:35:27 +0000409 * before all modules are destroyed.
410 * XXX If a __del__ or weakref callback is triggered here, and tries to
411 * XXX import a module, bad things can happen, because Python no
412 * XXX longer believes it's initialized.
413 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
414 * XXX is easy to provoke that way. I've also seen, e.g.,
415 * XXX Exception exceptions.ImportError: 'No module named sha'
416 * XXX in <function callback at 0x008F5718> ignored
417 * XXX but I'm unclear on exactly how that one happens. In any case,
418 * XXX I haven't seen a real-life report of either of these.
Neal Norwitz9589ee22006-03-04 19:01:22 +0000419 */
Guido van Rossume13ddc92003-04-17 17:29:22 +0000420 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000421#ifdef COUNT_ALLOCS
422 /* With COUNT_ALLOCS, it helps to run GC multiple times:
423 each collection might release some types from the type
424 list, so they become garbage. */
425 while (PyGC_Collect() > 0)
426 /* nothing */;
427#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000428
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000429 /* Destroy all modules */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000431
Guido van Rossume13ddc92003-04-17 17:29:22 +0000432 /* Collect final garbage. This disposes of cycles created by
Tim Peters1d7323e2003-12-01 21:35:27 +0000433 * new-style class definitions, for example.
434 * XXX This is disabled because it caused too many problems. If
435 * XXX a __del__ or weakref callback triggers here, Python code has
436 * XXX a hard time running, because even the sys module has been
437 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
438 * XXX One symptom is a sequence of information-free messages
439 * XXX coming from threads (if a __del__ or callback is invoked,
440 * XXX other threads can execute too, and any exception they encounter
441 * XXX triggers a comedy of errors as subsystem after subsystem
442 * XXX fails to find what it *expects* to find in sys to help report
443 * XXX the exception and consequent unexpected failures). I've also
444 * XXX seen segfaults then, after adding print statements to the
445 * XXX Python code getting called.
446 */
447#if 0
Guido van Rossume13ddc92003-04-17 17:29:22 +0000448 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000449#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000450
Guido van Rossum1707aad1997-12-08 23:43:45 +0000451 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
452 _PyImport_Fini();
453
454 /* Debugging stuff */
455#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000456 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000457#endif
458
Tim Peters62e97f02006-03-28 21:44:32 +0000459 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000460
Tim Peters9cf25ce2003-04-17 15:21:01 +0000461#ifdef Py_TRACE_REFS
462 /* Display all objects still alive -- this can invoke arbitrary
463 * __repr__ overrides, so requires a mostly-intact interpreter.
464 * Alas, a lot of stuff may still be alive now that will be cleaned
465 * up later.
466 */
Tim Peters269b2a62003-04-17 19:52:29 +0000467 if (Py_GETENV("PYTHONDUMPREFS"))
Tim Peters9cf25ce2003-04-17 15:21:01 +0000468 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000469#endif /* Py_TRACE_REFS */
470
Guido van Rossumd922fa42003-04-15 14:10:09 +0000471 /* Clear interpreter state */
Barry Warsawf242aa02000-05-25 23:09:49 +0000472 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000473
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000474 /* Now we decref the exception classes. After this point nothing
475 can raise an exception. That's okay, because each Fini() method
476 below has been checked to make sure no exceptions are ever
477 raised.
478 */
479
480 _PyExc_Fini();
481
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000482 /* Cleanup auto-thread-state */
483#ifdef WITH_THREAD
484 _PyGILState_Fini();
485#endif /* WITH_THREAD */
486
Guido van Rossumd922fa42003-04-15 14:10:09 +0000487 /* Delete current thread */
Barry Warsawf242aa02000-05-25 23:09:49 +0000488 PyThreadState_Swap(NULL);
489 PyInterpreterState_Delete(interp);
490
Guido van Rossumd922fa42003-04-15 14:10:09 +0000491 /* Sundry finalizers */
Guido van Rossumcc283f51997-08-05 02:22:03 +0000492 PyMethod_Fini();
493 PyFrame_Fini();
494 PyCFunction_Fini();
495 PyTuple_Fini();
Raymond Hettingerfb09f0e2004-10-07 03:58:07 +0000496 PyList_Fini();
Raymond Hettingerd7946662005-08-01 21:39:29 +0000497 PySet_Fini();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000498 PyString_Fini();
Christian Heimes3497f942008-05-26 12:29:14 +0000499 PyByteArray_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000500 PyInt_Fini();
501 PyFloat_Fini();
Christian Heimesf75dbef2008-02-08 00:11:31 +0000502 PyDict_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000503
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000504#ifdef Py_USING_UNICODE
505 /* Cleanup Unicode implementation */
506 _PyUnicode_Fini();
507#endif
508
Guido van Rossumcc283f51997-08-05 02:22:03 +0000509 /* XXX Still allocated:
510 - various static ad-hoc pointers to interned strings
511 - int and float free list blocks
512 - whatever various modules and libraries allocate
513 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000514
515 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000516
Tim Peters269b2a62003-04-17 19:52:29 +0000517#ifdef Py_TRACE_REFS
518 /* Display addresses (& refcnts) of all objects still alive.
519 * An address can be used to find the repr of the object, printed
520 * above by _Py_PrintReferences.
521 */
522 if (Py_GETENV("PYTHONDUMPREFS"))
523 _Py_PrintReferenceAddresses(stderr);
524#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000525#ifdef PYMALLOC_DEBUG
526 if (Py_GETENV("PYTHONMALLOCSTATS"))
527 _PyObject_DebugMallocStats();
528#endif
529
Guido van Rossumcc283f51997-08-05 02:22:03 +0000530 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000531}
532
533/* Create and initialize a new interpreter and thread, and return the
534 new thread. This requires that Py_Initialize() has been called
535 first.
536
537 Unsuccessful initialization yields a NULL pointer. Note that *no*
538 exception information is available even in this case -- the
539 exception information is held in the thread, and there is no
540 thread.
541
542 Locking: as above.
543
544*/
545
546PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000547Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000548{
549 PyInterpreterState *interp;
550 PyThreadState *tstate, *save_tstate;
551 PyObject *bimod, *sysmod;
552
553 if (!initialized)
554 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
555
556 interp = PyInterpreterState_New();
557 if (interp == NULL)
558 return NULL;
559
560 tstate = PyThreadState_New(interp);
561 if (tstate == NULL) {
562 PyInterpreterState_Delete(interp);
563 return NULL;
564 }
565
566 save_tstate = PyThreadState_Swap(tstate);
567
568 /* XXX The following is lax in error checking */
569
570 interp->modules = PyDict_New();
Collin Winter276887b2007-03-12 16:11:39 +0000571 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000572
573 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
574 if (bimod != NULL) {
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000575 interp->builtins = PyModule_GetDict(bimod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000576 if (interp->builtins == NULL)
577 goto handle_error;
Guido van Rossum4a1f39a1997-11-04 19:36:18 +0000578 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000579 }
580 sysmod = _PyImport_FindExtension("sys", "sys");
581 if (bimod != NULL && sysmod != NULL) {
582 interp->sysdict = PyModule_GetDict(sysmod);
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000583 if (interp->sysdict == NULL)
584 goto handle_error;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000585 Py_INCREF(interp->sysdict);
586 PySys_SetPath(Py_GetPath());
587 PyDict_SetItemString(interp->sysdict, "modules",
588 interp->modules);
Martin v. Löwisd7ceb222003-01-22 09:00:38 +0000589 _PyImportHooks_Init();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000590 initmain();
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000591 if (!Py_NoSiteFlag)
592 initsite();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000593 }
594
595 if (!PyErr_Occurred())
596 return tstate;
597
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000598handle_error:
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599 /* Oops, it didn't work. Undo it all. */
600
601 PyErr_Print();
602 PyThreadState_Clear(tstate);
603 PyThreadState_Swap(save_tstate);
604 PyThreadState_Delete(tstate);
605 PyInterpreterState_Delete(interp);
606
607 return NULL;
608}
609
610/* Delete an interpreter and its last thread. This requires that the
611 given thread state is current, that the thread has no remaining
612 frames, and that it is its interpreter's only remaining thread.
613 It is a fatal error to violate these constraints.
614
615 (Py_Finalize() doesn't have these constraints -- it zaps
616 everything, regardless.)
617
618 Locking: as above.
619
620*/
621
622void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000623Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624{
625 PyInterpreterState *interp = tstate->interp;
626
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000627 if (tstate != PyThreadState_GET())
Guido van Rossum25ce5661997-08-02 03:10:38 +0000628 Py_FatalError("Py_EndInterpreter: thread is not current");
629 if (tstate->frame != NULL)
630 Py_FatalError("Py_EndInterpreter: thread still has a frame");
631 if (tstate != interp->tstate_head || tstate->next != NULL)
632 Py_FatalError("Py_EndInterpreter: not the last thread");
633
634 PyImport_Cleanup();
635 PyInterpreterState_Clear(interp);
636 PyThreadState_Swap(NULL);
637 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000638}
639
640static char *progname = "python";
641
642void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000643Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000644{
645 if (pn && *pn)
646 progname = pn;
647}
648
649char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000651{
652 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000653}
654
Guido van Rossuma61691e1998-02-06 22:27:24 +0000655static char *default_home = NULL;
656
657void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000658Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000659{
660 default_home = home;
661}
662
663char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000664Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000665{
666 char *home = default_home;
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000667 if (home == NULL && !Py_IgnoreEnvironmentFlag)
668 home = Py_GETENV("PYTHONHOME");
Guido van Rossuma61691e1998-02-06 22:27:24 +0000669 return home;
670}
671
Guido van Rossum6135a871995-01-09 17:53:26 +0000672/* Create __main__ module */
673
674static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000675initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000676{
Guido van Rossum82598051997-03-05 00:20:32 +0000677 PyObject *m, *d;
678 m = PyImport_AddModule("__main__");
Guido van Rossum6135a871995-01-09 17:53:26 +0000679 if (m == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +0000680 Py_FatalError("can't create __main__ module");
681 d = PyModule_GetDict(m);
682 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
Guido van Rossum858cb731997-11-19 16:15:37 +0000683 PyObject *bimod = PyImport_ImportModule("__builtin__");
684 if (bimod == NULL ||
685 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
Guido van Rossum82598051997-03-05 00:20:32 +0000686 Py_FatalError("can't add __builtins__ to __main__");
Barry Warsaw3d05b1a1999-01-29 21:30:22 +0000687 Py_DECREF(bimod);
Guido van Rossum6135a871995-01-09 17:53:26 +0000688 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000689}
690
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000691/* Import the site module (not into __main__ though) */
692
693static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000694initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000695{
696 PyObject *m, *f;
697 m = PyImport_ImportModule("site");
698 if (m == NULL) {
699 f = PySys_GetObject("stderr");
700 if (Py_VerboseFlag) {
701 PyFile_WriteString(
702 "'import site' failed; traceback:\n", f);
703 PyErr_Print();
704 }
705 else {
706 PyFile_WriteString(
707 "'import site' failed; use -v for traceback\n", f);
708 PyErr_Clear();
709 }
710 }
711 else {
712 Py_DECREF(m);
713 }
714}
715
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000716/* Parse input from a file and execute it */
717
718int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000719PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000720 PyCompilerFlags *flags)
721{
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000722 if (filename == NULL)
723 filename = "???";
Guido van Rossum0df002c2000-08-27 19:21:52 +0000724 if (Py_FdIsInteractive(fp, filename)) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000725 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
Guido van Rossum0df002c2000-08-27 19:21:52 +0000726 if (closeit)
727 fclose(fp);
728 return err;
729 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000730 else
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000731 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000732}
733
734int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000735PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000736{
Guido van Rossum82598051997-03-05 00:20:32 +0000737 PyObject *v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000738 int ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000739 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000740
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000741 if (flags == NULL) {
742 flags = &local_flags;
Tim Peters5ba58662001-07-16 02:29:45 +0000743 local_flags.cf_flags = 0;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000744 }
Guido van Rossum82598051997-03-05 00:20:32 +0000745 v = PySys_GetObject("ps1");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000746 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000747 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
Guido van Rossum82598051997-03-05 00:20:32 +0000748 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000749 }
Guido van Rossum82598051997-03-05 00:20:32 +0000750 v = PySys_GetObject("ps2");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000751 if (v == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000752 PySys_SetObject("ps2", v = PyString_FromString("... "));
Guido van Rossum82598051997-03-05 00:20:32 +0000753 Py_XDECREF(v);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000754 }
755 for (;;) {
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000756 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
Tim Peters62e97f02006-03-28 21:44:32 +0000757 PRINT_TOTAL_REFS();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000758 if (ret == E_EOF)
759 return 0;
760 /*
761 if (ret == E_NOMEM)
762 return -1;
763 */
764 }
765}
766
Eric Smith7c478942008-03-18 23:45:49 +0000767#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000768/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000769#define PARSER_FLAGS(flags) \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000770 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Neal Norwitzca460d92006-09-06 06:28:06 +0000771 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000772#endif
773#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000774/* Keep an example of flags with future keyword support. */
775#define PARSER_FLAGS(flags) \
776 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000777 PyPARSE_DONT_IMPLY_DEDENT : 0) \
Christian Heimes3c608332008-03-26 22:01:37 +0000778 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
779 PyPARSE_PRINT_IS_FUNCTION : 0) \
780 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
781 PyPARSE_UNICODE_LITERALS : 0) \
782 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000783#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000784
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000785int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000786PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000787{
Guido van Rossum82598051997-03-05 00:20:32 +0000788 PyObject *m, *d, *v, *w;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +0000790 PyArena *arena;
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000791 char *ps1 = "", *ps2 = "";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000793
Guido van Rossum82598051997-03-05 00:20:32 +0000794 v = PySys_GetObject("ps1");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000795 if (v != NULL) {
796 v = PyObject_Str(v);
797 if (v == NULL)
798 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000799 else if (PyString_Check(v))
800 ps1 = PyString_AsString(v);
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000801 }
Guido van Rossum82598051997-03-05 00:20:32 +0000802 w = PySys_GetObject("ps2");
Guido van Rossumddc3fb51997-11-25 20:58:13 +0000803 if (w != NULL) {
804 w = PyObject_Str(w);
805 if (w == NULL)
806 PyErr_Clear();
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 else if (PyString_Check(w))
808 ps2 = PyString_AsString(w);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000809 }
Neal Norwitz9589ee22006-03-04 19:01:22 +0000810 arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000811 if (arena == NULL) {
812 Py_XDECREF(v);
813 Py_XDECREF(w);
814 return -1;
815 }
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000816 mod = PyParser_ASTFromFile(fp, filename,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 Py_single_input, ps1, ps2,
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000818 flags, &errcode, arena);
Guido van Rossum82598051997-03-05 00:20:32 +0000819 Py_XDECREF(v);
820 Py_XDECREF(w);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000822 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 if (errcode == E_EOF) {
824 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +0000825 return E_EOF;
826 }
Guido van Rossum82598051997-03-05 00:20:32 +0000827 PyErr_Print();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 return -1;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000829 }
Guido van Rossum82598051997-03-05 00:20:32 +0000830 m = PyImport_AddModule("__main__");
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000831 if (m == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +0000832 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000833 return -1;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000834 }
Guido van Rossum82598051997-03-05 00:20:32 +0000835 d = PyModule_GetDict(m);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000836 v = run_mod(mod, filename, d, d, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +0000837 PyArena_Free(arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000838 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000839 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000840 return -1;
841 }
Guido van Rossum82598051997-03-05 00:20:32 +0000842 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000843 if (Py_FlushLine())
844 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000845 return 0;
846}
847
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000848/* Check whether a file maybe a pyc file: Look at the extension,
849 the file type, and, if we may close it, at the first few bytes. */
850
851static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000852maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000853{
854 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
855 return 1;
856
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000857 /* Only look into the file if we are allowed to close it, since
858 it then should also be seekable. */
859 if (closeit) {
860 /* Read only two bytes of the magic. If the file was opened in
861 text mode, the bytes 3 and 4 of the magic (\r\n) might not
862 be read as they are on disk. */
863 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
864 unsigned char buf[2];
Tim Peters3e876562001-02-11 04:35:39 +0000865 /* Mess: In case of -x, the stream is NOT at its start now,
866 and ungetc() was used to push back the first newline,
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000867 which makes the current stream position formally undefined,
868 and a x-platform nightmare.
869 Unfortunately, we have no direct way to know whether -x
870 was specified. So we use a terrible hack: if the current
871 stream position is not 0, we assume -x was specified, and
872 give up. Bug 132850 on SourceForge spells out the
873 hopelessness of trying anything else (fseek and ftell
874 don't work predictably x-platform for text-mode files).
Tim Peters3e876562001-02-11 04:35:39 +0000875 */
Tim Peters3e876562001-02-11 04:35:39 +0000876 int ispyc = 0;
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000877 if (ftell(fp) == 0) {
878 if (fread(buf, 1, 2, fp) == 2 &&
Tim Petersd08e3822003-04-17 15:24:21 +0000879 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
Tim Peters6f5a4ef2001-02-17 22:02:34 +0000880 ispyc = 1;
881 rewind(fp);
882 }
Tim Peters3e876562001-02-11 04:35:39 +0000883 return ispyc;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000884 }
885 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000886}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000887
Guido van Rossum0df002c2000-08-27 19:21:52 +0000888int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000889PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000890 PyCompilerFlags *flags)
891{
Guido van Rossum82598051997-03-05 00:20:32 +0000892 PyObject *m, *d, *v;
Martin v. Löwis95292d62002-12-11 14:04:59 +0000893 const char *ext;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000894 int set_file_name = 0, ret;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000895
Guido van Rossum82598051997-03-05 00:20:32 +0000896 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000897 if (m == NULL)
898 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000899 d = PyModule_GetDict(m);
Fred Drake8ed02042002-10-17 21:24:58 +0000900 if (PyDict_GetItemString(d, "__file__") == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000901 PyObject *f = PyString_FromString(filename);
Fred Drake8ed02042002-10-17 21:24:58 +0000902 if (f == NULL)
903 return -1;
904 if (PyDict_SetItemString(d, "__file__", f) < 0) {
905 Py_DECREF(f);
906 return -1;
907 }
Georg Brandlaa2321b2007-03-07 00:40:28 +0000908 set_file_name = 1;
Fred Drake8ed02042002-10-17 21:24:58 +0000909 Py_DECREF(f);
910 }
Guido van Rossumfdef2711994-09-14 13:31:04 +0000911 ext = filename + strlen(filename) - 4;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000912 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000913 /* Try to run a pyc file. First, re-open in binary */
Guido van Rossum0df002c2000-08-27 19:21:52 +0000914 if (closeit)
915 fclose(fp);
Fred Drake8ed02042002-10-17 21:24:58 +0000916 if ((fp = fopen(filename, "rb")) == NULL) {
Guido van Rossumfdef2711994-09-14 13:31:04 +0000917 fprintf(stderr, "python: Can't reopen .pyc file\n");
Georg Brandlaa2321b2007-03-07 00:40:28 +0000918 ret = -1;
919 goto done;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000920 }
Guido van Rossum2a7f58d1997-04-02 05:28:38 +0000921 /* Turn on optimization if a .pyo file is given */
922 if (strcmp(ext, ".pyo") == 0)
923 Py_OptimizeFlag = 1;
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000924 v = run_pyc_file(fp, filename, d, d, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000925 } else {
Tim Petersd08e3822003-04-17 15:24:21 +0000926 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000927 closeit, flags);
Guido van Rossumfdef2711994-09-14 13:31:04 +0000928 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000929 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000930 PyErr_Print();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000931 ret = -1;
932 goto done;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000933 }
Guido van Rossum82598051997-03-05 00:20:32 +0000934 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000935 if (Py_FlushLine())
936 PyErr_Clear();
Georg Brandlaa2321b2007-03-07 00:40:28 +0000937 ret = 0;
938 done:
939 if (set_file_name && PyDict_DelItemString(d, "__file__"))
940 PyErr_Clear();
941 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000942}
943
944int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000945PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000946{
Guido van Rossum82598051997-03-05 00:20:32 +0000947 PyObject *m, *d, *v;
948 m = PyImport_AddModule("__main__");
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000949 if (m == NULL)
950 return -1;
Guido van Rossum82598051997-03-05 00:20:32 +0000951 d = PyModule_GetDict(m);
Guido van Rossum393661d2001-08-31 17:40:15 +0000952 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000953 if (v == NULL) {
Guido van Rossum82598051997-03-05 00:20:32 +0000954 PyErr_Print();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000955 return -1;
956 }
Guido van Rossum82598051997-03-05 00:20:32 +0000957 Py_DECREF(v);
Guido van Rossum78a1ed31997-05-22 22:35:04 +0000958 if (Py_FlushLine())
959 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000960 return 0;
961}
962
Barry Warsaw035574d1997-08-29 22:07:17 +0000963static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000964parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
965 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000966{
967 long hold;
968 PyObject *v;
969
970 /* old style errors */
971 if (PyTuple_Check(err))
Neal Norwitz78293352002-04-01 01:41:20 +0000972 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
Neal Norwitz9589ee22006-03-04 19:01:22 +0000973 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000974
975 /* new style errors. `err' is an instance */
976
977 if (! (v = PyObject_GetAttrString(err, "msg")))
978 goto finally;
979 *message = v;
980
981 if (!(v = PyObject_GetAttrString(err, "filename")))
982 goto finally;
983 if (v == Py_None)
984 *filename = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000985 else if (! (*filename = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +0000986 goto finally;
987
988 Py_DECREF(v);
989 if (!(v = PyObject_GetAttrString(err, "lineno")))
990 goto finally;
991 hold = PyInt_AsLong(v);
992 Py_DECREF(v);
993 v = NULL;
994 if (hold < 0 && PyErr_Occurred())
995 goto finally;
996 *lineno = (int)hold;
997
998 if (!(v = PyObject_GetAttrString(err, "offset")))
999 goto finally;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001000 if (v == Py_None) {
1001 *offset = -1;
1002 Py_DECREF(v);
1003 v = NULL;
1004 } else {
1005 hold = PyInt_AsLong(v);
1006 Py_DECREF(v);
1007 v = NULL;
1008 if (hold < 0 && PyErr_Occurred())
1009 goto finally;
1010 *offset = (int)hold;
1011 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001012
1013 if (!(v = PyObject_GetAttrString(err, "text")))
1014 goto finally;
1015 if (v == Py_None)
1016 *text = NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001017 else if (! (*text = PyString_AsString(v)))
Barry Warsaw035574d1997-08-29 22:07:17 +00001018 goto finally;
1019 Py_DECREF(v);
1020 return 1;
1021
1022finally:
1023 Py_XDECREF(v);
1024 return 0;
1025}
1026
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001027void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001028PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001029{
Guido van Rossuma61691e1998-02-06 22:27:24 +00001030 PyErr_PrintEx(1);
1031}
1032
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001033static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001034print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001035{
1036 char *nl;
1037 if (offset >= 0) {
1038 if (offset > 0 && offset == (int)strlen(text))
1039 offset--;
1040 for (;;) {
1041 nl = strchr(text, '\n');
1042 if (nl == NULL || nl-text >= offset)
1043 break;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044 offset -= (int)(nl+1-text);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001045 text = nl+1;
1046 }
1047 while (*text == ' ' || *text == '\t') {
1048 text++;
1049 offset--;
1050 }
1051 }
1052 PyFile_WriteString(" ", f);
1053 PyFile_WriteString(text, f);
1054 if (*text == '\0' || text[strlen(text)-1] != '\n')
1055 PyFile_WriteString("\n", f);
1056 if (offset == -1)
1057 return;
1058 PyFile_WriteString(" ", f);
1059 offset--;
1060 while (offset > 0) {
1061 PyFile_WriteString(" ", f);
1062 offset--;
1063 }
1064 PyFile_WriteString("^\n", f);
1065}
1066
Guido van Rossum66e8e862001-03-23 17:54:43 +00001067static void
1068handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001069{
Neal Norwitz9589ee22006-03-04 19:01:22 +00001070 PyObject *exception, *value, *tb;
1071 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001072
Georg Brandl49aafc92007-03-07 00:34:46 +00001073 if (Py_InspectFlag)
1074 /* Don't exit if -i flag was given. This flag is set to 0
1075 * when entering interactive mode for inspecting. */
1076 return;
1077
Guido van Rossum66e8e862001-03-23 17:54:43 +00001078 PyErr_Fetch(&exception, &value, &tb);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001079 if (Py_FlushLine())
1080 PyErr_Clear();
1081 fflush(stdout);
1082 if (value == NULL || value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001083 goto done;
Brett Cannonbf364092006-03-01 04:25:17 +00001084 if (PyExceptionInstance_Check(value)) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001085 /* The error code should be in the `code' attribute. */
1086 PyObject *code = PyObject_GetAttrString(value, "code");
1087 if (code) {
1088 Py_DECREF(value);
1089 value = code;
1090 if (value == Py_None)
Tim Peterscf615b52003-04-19 18:47:02 +00001091 goto done;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001092 }
1093 /* If we failed to dig out the 'code' attribute,
1094 just let the else clause below print the error. */
1095 }
1096 if (PyInt_Check(value))
Tim Peterscf615b52003-04-19 18:47:02 +00001097 exitcode = (int)PyInt_AsLong(value);
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001098 else {
1099 PyObject_Print(value, stderr, Py_PRINT_RAW);
1100 PySys_WriteStderr("\n");
Tim Peterscf615b52003-04-19 18:47:02 +00001101 exitcode = 1;
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001102 }
Tim Peterscf615b52003-04-19 18:47:02 +00001103 done:
Neal Norwitz9589ee22006-03-04 19:01:22 +00001104 /* Restore and clear the exception info, in order to properly decref
1105 * the exception, value, and traceback. If we just exit instead,
1106 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1107 * some finalizers from running.
1108 */
Tim Peterscf615b52003-04-19 18:47:02 +00001109 PyErr_Restore(exception, value, tb);
1110 PyErr_Clear();
1111 Py_Exit(exitcode);
1112 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001113}
1114
1115void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001116PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001117{
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001118 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001119
1120 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1121 handle_system_exit();
1122 }
Guido van Rossum82598051997-03-05 00:20:32 +00001123 PyErr_Fetch(&exception, &v, &tb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 if (exception == NULL)
1125 return;
Barry Warsaw035574d1997-08-29 22:07:17 +00001126 PyErr_NormalizeException(&exception, &v, &tb);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001127 if (exception == NULL)
Guido van Rossum296b4751997-05-23 00:19:20 +00001128 return;
Georg Brandl3c0fd562008-07-05 10:07:18 +00001129 /* Now we know v != NULL too */
Guido van Rossuma61691e1998-02-06 22:27:24 +00001130 if (set_sys_last_vars) {
1131 PySys_SetObject("last_type", exception);
1132 PySys_SetObject("last_value", v);
1133 PySys_SetObject("last_traceback", tb);
1134 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001135 hook = PySys_GetObject("excepthook");
1136 if (hook) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001137 PyObject *args = PyTuple_Pack(3,
Guido van Rossum9d785502006-03-07 18:31:44 +00001138 exception, v, tb ? tb : Py_None);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001139 PyObject *result = PyEval_CallObject(hook, args);
1140 if (result == NULL) {
1141 PyObject *exception2, *v2, *tb2;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001142 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1143 handle_system_exit();
1144 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001145 PyErr_Fetch(&exception2, &v2, &tb2);
1146 PyErr_NormalizeException(&exception2, &v2, &tb2);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001147 /* It should not be possible for exception2 or v2
1148 to be NULL. However PyErr_Display() can't
1149 tolerate NULLs, so just be safe. */
1150 if (exception2 == NULL) {
1151 exception2 = Py_None;
1152 Py_INCREF(exception2);
1153 }
1154 if (v2 == NULL) {
1155 v2 = Py_None;
1156 Py_INCREF(v2);
1157 }
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001158 if (Py_FlushLine())
1159 PyErr_Clear();
1160 fflush(stdout);
1161 PySys_WriteStderr("Error in sys.excepthook:\n");
1162 PyErr_Display(exception2, v2, tb2);
1163 PySys_WriteStderr("\nOriginal exception was:\n");
1164 PyErr_Display(exception, v, tb);
Neal Norwitza5e4f222006-07-17 00:59:04 +00001165 Py_DECREF(exception2);
1166 Py_DECREF(v2);
Jeremy Hylton07028582001-12-07 15:35:35 +00001167 Py_XDECREF(tb2);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001168 }
1169 Py_XDECREF(result);
1170 Py_XDECREF(args);
1171 } else {
1172 PySys_WriteStderr("sys.excepthook is missing\n");
1173 PyErr_Display(exception, v, tb);
1174 }
1175 Py_XDECREF(exception);
1176 Py_XDECREF(v);
1177 Py_XDECREF(tb);
1178}
1179
Richard Jones7b9558d2006-05-27 12:29:24 +00001180void
1181PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001182{
1183 int err = 0;
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001184 PyObject *f = PySys_GetObject("stderr");
Armin Rigo5d2c6832004-03-22 20:16:58 +00001185 Py_INCREF(value);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001186 if (f == NULL)
1187 fprintf(stderr, "lost sys.stderr\n");
1188 else {
Guido van Rossum0829c751998-02-28 04:31:39 +00001189 if (Py_FlushLine())
1190 PyErr_Clear();
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001191 fflush(stdout);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001192 if (tb && tb != Py_None)
1193 err = PyTraceBack_Print(tb, f);
Barry Warsaw36b8f941997-08-26 18:09:48 +00001194 if (err == 0 &&
Armin Rigo5d2c6832004-03-22 20:16:58 +00001195 PyObject_HasAttrString(value, "print_file_and_line"))
Barry Warsaw36b8f941997-08-26 18:09:48 +00001196 {
Guido van Rossum82598051997-03-05 00:20:32 +00001197 PyObject *message;
Martin v. Löwis95292d62002-12-11 14:04:59 +00001198 const char *filename, *text;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001199 int lineno, offset;
Armin Rigo5d2c6832004-03-22 20:16:58 +00001200 if (!parse_syntax_error(value, &message, &filename,
Barry Warsaw035574d1997-08-29 22:07:17 +00001201 &lineno, &offset, &text))
Guido van Rossum82598051997-03-05 00:20:32 +00001202 PyErr_Clear();
Guido van Rossuma110aa61994-08-29 12:50:44 +00001203 else {
1204 char buf[10];
Guido van Rossum82598051997-03-05 00:20:32 +00001205 PyFile_WriteString(" File \"", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001206 if (filename == NULL)
Guido van Rossum82598051997-03-05 00:20:32 +00001207 PyFile_WriteString("<string>", f);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001208 else
Guido van Rossum82598051997-03-05 00:20:32 +00001209 PyFile_WriteString(filename, f);
1210 PyFile_WriteString("\", line ", f);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +00001211 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
Guido van Rossum82598051997-03-05 00:20:32 +00001212 PyFile_WriteString(buf, f);
1213 PyFile_WriteString("\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001214 if (text != NULL)
1215 print_error_text(f, offset, text);
Armin Rigo5d2c6832004-03-22 20:16:58 +00001216 Py_DECREF(value);
1217 value = message;
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001218 /* Can't be bothered to check all those
1219 PyFile_WriteString() calls */
1220 if (PyErr_Occurred())
1221 err = -1;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001222 }
1223 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001224 if (err) {
1225 /* Don't do anything else */
1226 }
Brett Cannonbf364092006-03-01 04:25:17 +00001227 else if (PyExceptionClass_Check(exception)) {
Richard Jones7b9558d2006-05-27 12:29:24 +00001228 PyObject* moduleName;
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001229 char* className = PyExceptionClass_Name(exception);
1230 if (className != NULL) {
1231 char *dot = strrchr(className, '.');
1232 if (dot != NULL)
1233 className = dot+1;
1234 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001235
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001236 moduleName = PyObject_GetAttrString(exception, "__module__");
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001237 if (moduleName == NULL)
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001238 err = PyFile_WriteString("<unknown>", f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001239 else {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001240 char* modstr = PyString_AsString(moduleName);
Tim Petersd08e3822003-04-17 15:24:21 +00001241 if (modstr && strcmp(modstr, "exceptions"))
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001242 {
1243 err = PyFile_WriteString(modstr, f);
1244 err += PyFile_WriteString(".", f);
1245 }
Richard Jones7b9558d2006-05-27 12:29:24 +00001246 Py_DECREF(moduleName);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001247 }
1248 if (err == 0) {
1249 if (className == NULL)
1250 err = PyFile_WriteString("<unknown>", f);
1251 else
Brett Cannonbf364092006-03-01 04:25:17 +00001252 err = PyFile_WriteString(className, f);
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001253 }
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001254 }
1255 else
1256 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
Brett Cannon2e63b732006-03-02 18:34:57 +00001257 if (err == 0 && (value != Py_None)) {
1258 PyObject *s = PyObject_Str(value);
1259 /* only print colon if the str() of the
1260 object is not the empty string
1261 */
1262 if (s == NULL)
1263 err = -1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001264 else if (!PyString_Check(s) ||
1265 PyString_GET_SIZE(s) != 0)
Brett Cannon2e63b732006-03-02 18:34:57 +00001266 err = PyFile_WriteString(": ", f);
1267 if (err == 0)
1268 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1269 Py_XDECREF(s);
Guido van Rossum262e1241995-02-07 15:30:45 +00001270 }
Georg Brandl7b9c5552007-03-12 14:30:05 +00001271 /* try to write a newline in any case */
1272 err += PyFile_WriteString("\n", f);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001273 }
Armin Rigo5d2c6832004-03-22 20:16:58 +00001274 Py_DECREF(value);
Guido van Rossum78a1ed31997-05-22 22:35:04 +00001275 /* If an error happened here, don't show it.
1276 XXX This is wrong, but too many callers rely on this behavior. */
1277 if (err != 0)
1278 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001279}
1280
Guido van Rossum82598051997-03-05 00:20:32 +00001281PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001282PyRun_StringFlags(const char *str, int start, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001284{
Neal Norwitze92fba02006-03-04 18:52:26 +00001285 PyObject *ret = NULL;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001286 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001287 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001288 if (arena == NULL)
1289 return NULL;
1290
1291 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
Neal Norwitze92fba02006-03-04 18:52:26 +00001292 if (mod != NULL)
1293 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001294 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001296}
1297
1298PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001299PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001300 PyObject *locals, int closeit, PyCompilerFlags *flags)
1301{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 PyObject *ret;
Neal Norwitzd12bd012006-07-21 07:59:47 +00001303 mod_ty mod;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001304 PyArena *arena = PyArena_New();
Neal Norwitzd12bd012006-07-21 07:59:47 +00001305 if (arena == NULL)
1306 return NULL;
1307
1308 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1309 flags, NULL, arena);
Georg Brandl098cd692007-03-06 12:17:50 +00001310 if (closeit)
1311 fclose(fp);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001312 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001313 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001315 }
Neal Norwitze92fba02006-03-04 18:52:26 +00001316 ret = run_mod(mod, filename, globals, locals, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001317 PyArena_Free(arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001319}
1320
Guido van Rossum82598051997-03-05 00:20:32 +00001321static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001323 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001324{
Guido van Rossum82598051997-03-05 00:20:32 +00001325 PyCodeObject *co;
1326 PyObject *v;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001327 co = PyAST_Compile(mod, filename, flags, arena);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001328 if (co == NULL)
1329 return NULL;
Guido van Rossum82598051997-03-05 00:20:32 +00001330 v = PyEval_EvalCode(co, globals, locals);
1331 Py_DECREF(co);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001332 return v;
1333}
1334
Guido van Rossum82598051997-03-05 00:20:32 +00001335static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001336run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001338{
Guido van Rossum82598051997-03-05 00:20:32 +00001339 PyCodeObject *co;
1340 PyObject *v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001341 long magic;
Fred Drakee8de31c2000-08-31 05:38:39 +00001342 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001343
Guido van Rossum82598051997-03-05 00:20:32 +00001344 magic = PyMarshal_ReadLongFromFile(fp);
1345 if (magic != PyImport_GetMagicNumber()) {
1346 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001347 "Bad magic number in .pyc file");
1348 return NULL;
1349 }
Guido van Rossum82598051997-03-05 00:20:32 +00001350 (void) PyMarshal_ReadLongFromFile(fp);
Tim Petersd9b9ac82001-01-28 00:27:39 +00001351 v = PyMarshal_ReadLastObjectFromFile(fp);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001352 fclose(fp);
Guido van Rossum82598051997-03-05 00:20:32 +00001353 if (v == NULL || !PyCode_Check(v)) {
1354 Py_XDECREF(v);
1355 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumfdef2711994-09-14 13:31:04 +00001356 "Bad code object in .pyc file");
1357 return NULL;
1358 }
Guido van Rossum82598051997-03-05 00:20:32 +00001359 co = (PyCodeObject *)v;
1360 v = PyEval_EvalCode(co, globals, locals);
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00001361 if (v && flags)
1362 flags->cf_flags |= (co->co_flags & PyCF_MASK);
Guido van Rossum82598051997-03-05 00:20:32 +00001363 Py_DECREF(co);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001364 return v;
1365}
1366
Guido van Rossum82598051997-03-05 00:20:32 +00001367PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001368Py_CompileStringFlags(const char *str, const char *filename, int start,
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001369 PyCompilerFlags *flags)
1370{
Guido van Rossum82598051997-03-05 00:20:32 +00001371 PyCodeObject *co;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001372 mod_ty mod;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001373 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001374 if (arena == NULL)
1375 return NULL;
1376
1377 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001378 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001379 PyArena_Free(arena);
Guido van Rossum5b722181993-03-30 17:46:03 +00001380 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001381 }
Martin v. Löwis2b366e42006-02-26 22:12:35 +00001382 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001383 PyObject *result = PyAST_mod2obj(mod);
1384 PyArena_Free(arena);
1385 return result;
1386 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001387 co = PyAST_Compile(mod, filename, flags, arena);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001388 PyArena_Free(arena);
Guido van Rossum82598051997-03-05 00:20:32 +00001389 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001390}
1391
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001392struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001393Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001394{
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001395 struct symtable *st;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001396 mod_ty mod;
Christian Heimes3c608332008-03-26 22:01:37 +00001397 PyCompilerFlags flags;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001398 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001399 if (arena == NULL)
1400 return NULL;
1401
Christian Heimes7f23d862008-03-26 22:51:58 +00001402 flags.cf_flags = 0;
1403
Christian Heimes3c608332008-03-26 22:01:37 +00001404 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001405 if (mod == NULL) {
Neal Norwitz9589ee22006-03-04 19:01:22 +00001406 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001407 return NULL;
Neal Norwitz9589ee22006-03-04 19:01:22 +00001408 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 st = PySymtable_Build(mod, filename, 0);
Neal Norwitz9589ee22006-03-04 19:01:22 +00001410 PyArena_Free(arena);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001411 return st;
1412}
1413
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414/* Preferred access to parser is through AST. */
1415mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001416PyParser_ASTFromString(const char *s, const char *filename, int start,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001417 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001420 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001422 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001423
1424 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001425 &_PyParser_Grammar, start, &err,
Christian Heimes3c608332008-03-26 22:01:37 +00001426 &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001427 if (flags == NULL) {
1428 localflags.cf_flags = 0;
1429 flags = &localflags;
1430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001432 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001433 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 PyNode_Free(n);
1435 return mod;
1436 }
1437 else {
1438 err_input(&err);
1439 return NULL;
1440 }
1441}
1442
1443mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001444PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001445 char *ps2, PyCompilerFlags *flags, int *errcode,
Neal Norwitz9589ee22006-03-04 19:01:22 +00001446 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 mod_ty mod;
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001449 PyCompilerFlags localflags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 perrdetail err;
Amaury Forgeot d'Arcdf70e052008-03-26 23:07:43 +00001451 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001452
Christian Heimes3c608332008-03-26 22:01:37 +00001453 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1454 start, ps1, ps2, &err, &iflags);
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001455 if (flags == NULL) {
1456 localflags.cf_flags = 0;
1457 flags = &localflags;
1458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 if (n) {
Benjamin Petersondcee09d2008-10-31 02:16:05 +00001460 flags->cf_flags |= iflags & PyCF_MASK;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001461 mod = PyAST_FromNode(n, flags, filename, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 PyNode_Free(n);
1463 return mod;
1464 }
1465 else {
1466 err_input(&err);
1467 if (errcode)
1468 *errcode = err.error;
1469 return NULL;
1470 }
1471}
1472
Guido van Rossuma110aa61994-08-29 12:50:44 +00001473/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001474
Guido van Rossuma110aa61994-08-29 12:50:44 +00001475node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001476PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001477{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001478 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001479 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1480 start, NULL, NULL, &err, flags);
Guido van Rossuma110aa61994-08-29 12:50:44 +00001481 if (n == NULL)
1482 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001483
Guido van Rossuma110aa61994-08-29 12:50:44 +00001484 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001485}
1486
Guido van Rossuma110aa61994-08-29 12:50:44 +00001487/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001488
Guido van Rossuma110aa61994-08-29 12:50:44 +00001489node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001490PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001491{
Tim Petersfe2127d2001-07-16 05:37:24 +00001492 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001493 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1494 start, &err, flags);
Tim Petersfe2127d2001-07-16 05:37:24 +00001495 if (n == NULL)
1496 err_input(&err);
1497 return n;
1498}
1499
1500node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001501PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Thomas Heller6b17abf2002-07-09 09:23:27 +00001502 int start, int flags)
1503{
Thomas Heller6b17abf2002-07-09 09:23:27 +00001504 perrdetail err;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001505 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1506 &_PyParser_Grammar, start, &err, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001507 if (n == NULL)
1508 err_input(&err);
1509 return n;
1510}
1511
1512node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001513PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001514{
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001515 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001516}
1517
Guido van Rossum66ebd912003-04-17 16:02:26 +00001518/* May want to move a more generalized form of this to parsetok.c or
1519 even parser modules. */
1520
1521void
1522PyParser_SetError(perrdetail *err)
1523{
1524 err_input(err);
1525}
1526
Guido van Rossuma110aa61994-08-29 12:50:44 +00001527/* Set the error appropriate to the given input error code (see errcode.h) */
1528
1529static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001530err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001531{
Fred Drake85f36392000-07-11 17:53:00 +00001532 PyObject *v, *w, *errtype;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001533 PyObject* u = NULL;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001534 char *msg = NULL;
Fred Drake85f36392000-07-11 17:53:00 +00001535 errtype = PyExc_SyntaxError;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001536 switch (err->error) {
1537 case E_SYNTAX:
Fred Drake85f36392000-07-11 17:53:00 +00001538 errtype = PyExc_IndentationError;
1539 if (err->expected == INDENT)
1540 msg = "expected an indented block";
1541 else if (err->token == INDENT)
1542 msg = "unexpected indent";
1543 else if (err->token == DEDENT)
1544 msg = "unexpected unindent";
1545 else {
1546 errtype = PyExc_SyntaxError;
1547 msg = "invalid syntax";
1548 }
Guido van Rossuma110aa61994-08-29 12:50:44 +00001549 break;
1550 case E_TOKEN:
1551 msg = "invalid token";
Guido van Rossuma110aa61994-08-29 12:50:44 +00001552 break;
Skip Montanaro118ec702002-08-15 01:20:16 +00001553 case E_EOFS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001554 msg = "EOF while scanning triple-quoted string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001555 break;
1556 case E_EOLS:
Georg Brandlb52a74b2008-05-11 15:07:39 +00001557 msg = "EOL while scanning string literal";
Skip Montanaro118ec702002-08-15 01:20:16 +00001558 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001559 case E_INTR:
Michael W. Hudson30ea2f22004-07-07 17:44:12 +00001560 if (!PyErr_Occurred())
1561 PyErr_SetNone(PyExc_KeyboardInterrupt);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001562 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001563 case E_NOMEM:
Guido van Rossum82598051997-03-05 00:20:32 +00001564 PyErr_NoMemory();
Georg Brandl1ad108d2008-07-19 10:08:55 +00001565 goto cleanup;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001566 case E_EOF:
1567 msg = "unexpected EOF while parsing";
1568 break;
Fred Drake85f36392000-07-11 17:53:00 +00001569 case E_TABSPACE:
1570 errtype = PyExc_TabError;
Guido van Rossum560e8ad1998-04-10 19:43:42 +00001571 msg = "inconsistent use of tabs and spaces in indentation";
1572 break;
Jeremy Hylton94988062000-06-20 19:10:44 +00001573 case E_OVERFLOW:
1574 msg = "expression too long";
1575 break;
Fred Drake85f36392000-07-11 17:53:00 +00001576 case E_DEDENT:
1577 errtype = PyExc_IndentationError;
1578 msg = "unindent does not match any outer indentation level";
1579 break;
1580 case E_TOODEEP:
1581 errtype = PyExc_IndentationError;
1582 msg = "too many levels of indentation";
1583 break;
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001584 case E_DECODE: {
1585 PyObject *type, *value, *tb;
1586 PyErr_Fetch(&type, &value, &tb);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001587 if (value != NULL) {
Martin v. Löwisd35edda2005-08-24 08:39:24 +00001588 u = PyObject_Str(value);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001589 if (u != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001590 msg = PyString_AsString(u);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001591 }
1592 }
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001593 if (msg == NULL)
1594 msg = "unknown decode error";
Neal Norwitzdb83eb32005-12-18 05:29:30 +00001595 Py_XDECREF(type);
1596 Py_XDECREF(value);
Neal Norwitz40d37812005-10-02 01:48:49 +00001597 Py_XDECREF(tb);
Martin v. Löwisc2632a52004-07-21 05:35:02 +00001598 break;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001599 }
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001600 case E_LINECONT:
1601 msg = "unexpected character after line continuation character";
1602 break;
Guido van Rossuma110aa61994-08-29 12:50:44 +00001603 default:
1604 fprintf(stderr, "error=%d\n", err->error);
1605 msg = "unknown parsing error";
1606 break;
1607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 v = Py_BuildValue("(ziiz)", err->filename,
1609 err->lineno, err->offset, err->text);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 w = NULL;
1611 if (v != NULL)
1612 w = Py_BuildValue("(sO)", msg, v);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001613 Py_XDECREF(u);
Guido van Rossum41318302001-03-23 04:01:07 +00001614 Py_XDECREF(v);
Fred Drake85f36392000-07-11 17:53:00 +00001615 PyErr_SetObject(errtype, w);
Guido van Rossum82598051997-03-05 00:20:32 +00001616 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001617cleanup:
1618 if (err->text != NULL) {
1619 PyObject_FREE(err->text);
1620 err->text = NULL;
1621 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001622}
1623
1624/* Print fatal error message and abort */
1625
1626void
Tim Peters7c321a82002-07-09 02:57:01 +00001627Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001628{
Guido van Rossum83dd6c31994-09-29 09:38:33 +00001629 fprintf(stderr, "Fatal Python error: %s\n", msg);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001630#ifdef MS_WINDOWS
Guido van Rossum23c94461997-05-22 20:21:30 +00001631 OutputDebugString("Fatal Python error: ");
Guido van Rossuma44823b1995-03-14 15:01:17 +00001632 OutputDebugString(msg);
1633 OutputDebugString("\n");
Guido van Rossum0ba35361998-08-13 13:33:16 +00001634#ifdef _DEBUG
1635 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001636#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001637#endif /* MS_WINDOWS */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001638 abort();
1639}
1640
1641/* Clean up and exit */
1642
Guido van Rossuma110aa61994-08-29 12:50:44 +00001643#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001644#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001645#endif
1646
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001647#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001648static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001649static int nexitfuncs = 0;
1650
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001651int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001652{
1653 if (nexitfuncs >= NEXITFUNCS)
1654 return -1;
1655 exitfuncs[nexitfuncs++] = func;
1656 return 0;
1657}
1658
Guido van Rossumcc283f51997-08-05 02:22:03 +00001659static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001660call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001661{
Guido van Rossum82598051997-03-05 00:20:32 +00001662 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001663
1664 if (exitfunc) {
Fred Drake6a12d8d2001-03-23 17:34:02 +00001665 PyObject *res;
Guido van Rossum82598051997-03-05 00:20:32 +00001666 Py_INCREF(exitfunc);
1667 PySys_SetObject("exitfunc", (PyObject *)NULL);
1668 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
Guido van Rossum59bff391992-09-03 20:28:00 +00001669 if (res == NULL) {
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001670 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1671 PySys_WriteStderr("Error in sys.exitfunc:\n");
1672 }
Guido van Rossum82598051997-03-05 00:20:32 +00001673 PyErr_Print();
Guido van Rossum59bff391992-09-03 20:28:00 +00001674 }
Guido van Rossum82598051997-03-05 00:20:32 +00001675 Py_DECREF(exitfunc);
Guido van Rossum59bff391992-09-03 20:28:00 +00001676 }
1677
Guido van Rossum0829c751998-02-28 04:31:39 +00001678 if (Py_FlushLine())
1679 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001680}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001681
Guido van Rossumcc283f51997-08-05 02:22:03 +00001682static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001683call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001684{
Guido van Rossum1662dd51994-09-07 14:38:28 +00001685 while (nexitfuncs > 0)
1686 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001687
1688 fflush(stdout);
1689 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001690}
1691
1692void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001693Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001694{
Guido van Rossumcc283f51997-08-05 02:22:03 +00001695 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001696
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001697 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001698}
1699
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001700static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001701initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001702{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001703#ifdef SIGPIPE
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001704 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001705#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001706#ifdef SIGXFZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001707 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001708#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001709#ifdef SIGXFSZ
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001710 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001711#endif
Guido van Rossum82598051997-03-05 00:20:32 +00001712 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001713}
1714
Guido van Rossum7433b121997-02-14 19:45:36 +00001715
1716/*
1717 * The file descriptor fd is considered ``interactive'' if either
1718 * a) isatty(fd) is TRUE, or
1719 * b) the -i flag was given, and the filename associated with
1720 * the descriptor is NULL or "<stdin>" or "???".
1721 */
1722int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001723Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001724{
1725 if (isatty((int)fileno(fp)))
1726 return 1;
1727 if (!Py_InteractiveFlag)
1728 return 0;
1729 return (filename == NULL) ||
1730 (strcmp(filename, "<stdin>") == 0) ||
1731 (strcmp(filename, "???") == 0);
1732}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001733
1734
Tim Petersd08e3822003-04-17 15:24:21 +00001735#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001736#if defined(WIN32) && defined(_MSC_VER)
1737
1738/* Stack checking for Microsoft C */
1739
1740#include <malloc.h>
1741#include <excpt.h>
1742
Fred Drakee8de31c2000-08-31 05:38:39 +00001743/*
1744 * Return non-zero when we run out of memory on the stack; zero otherwise.
1745 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001746int
Fred Drake399739f2000-08-31 05:52:44 +00001747PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001748{
1749 __try {
Tim Peters92e4dd82002-10-05 01:47:34 +00001750 /* alloca throws a stack overflow exception if there's
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001751 not enough space left on the stack */
Tim Peters92e4dd82002-10-05 01:47:34 +00001752 alloca(PYOS_STACK_MARGIN * sizeof(void*));
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001753 return 0;
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001754 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1755 EXCEPTION_EXECUTE_HANDLER :
1756 EXCEPTION_CONTINUE_SEARCH) {
1757 int errcode = _resetstkoflw();
Amaury Forgeot d'Arcdc619012008-11-22 20:01:18 +00001758 if (errcode == 0)
Kristján Valur Jónsson52999352008-02-18 17:40:47 +00001759 {
1760 Py_FatalError("Could not reset the stack!");
1761 }
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001762 }
1763 return 1;
1764}
1765
1766#endif /* WIN32 && _MSC_VER */
1767
1768/* Alternate implementations can be added here... */
1769
1770#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001771
1772
1773/* Wrappers around sigaction() or signal(). */
1774
1775PyOS_sighandler_t
1776PyOS_getsig(int sig)
1777{
1778#ifdef HAVE_SIGACTION
1779 struct sigaction context;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001780 if (sigaction(sig, NULL, &context) == -1)
1781 return SIG_ERR;
Guido van Rossum6f256182000-09-16 16:32:19 +00001782 return context.sa_handler;
1783#else
1784 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001785/* Special signal handling for the secure CRT in Visual Studio 2005 */
1786#if defined(_MSC_VER) && _MSC_VER >= 1400
1787 switch (sig) {
1788 /* Only these signals are valid */
1789 case SIGINT:
1790 case SIGILL:
1791 case SIGFPE:
1792 case SIGSEGV:
1793 case SIGTERM:
1794 case SIGBREAK:
1795 case SIGABRT:
1796 break;
1797 /* Don't call signal() with other values or it will assert */
1798 default:
1799 return SIG_ERR;
1800 }
1801#endif /* _MSC_VER && _MSC_VER >= 1400 */
Guido van Rossum6f256182000-09-16 16:32:19 +00001802 handler = signal(sig, SIG_IGN);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001803 if (handler != SIG_ERR)
1804 signal(sig, handler);
Guido van Rossum6f256182000-09-16 16:32:19 +00001805 return handler;
1806#endif
1807}
1808
1809PyOS_sighandler_t
1810PyOS_setsig(int sig, PyOS_sighandler_t handler)
1811{
1812#ifdef HAVE_SIGACTION
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001813 struct sigaction context, ocontext;
Guido van Rossum6f256182000-09-16 16:32:19 +00001814 context.sa_handler = handler;
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001815 sigemptyset(&context.sa_mask);
1816 context.sa_flags = 0;
1817 if (sigaction(sig, &context, &ocontext) == -1)
1818 return SIG_ERR;
1819 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001820#else
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001821 PyOS_sighandler_t oldhandler;
1822 oldhandler = signal(sig, handler);
1823#ifdef HAVE_SIGINTERRUPT
1824 siginterrupt(sig, 1);
1825#endif
1826 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001827#endif
1828}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829
1830/* Deprecated C API functions still provided for binary compatiblity */
1831
1832#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001833PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1835{
1836 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1837}
1838
Thomas Heller1b046642006-04-18 18:51:06 +00001839#undef PyParser_SimpleParseString
1840PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841PyParser_SimpleParseString(const char *str, int start)
1842{
1843 return PyParser_SimpleParseStringFlags(str, start, 0);
1844}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001845
Thomas Heller1b046642006-04-18 18:51:06 +00001846#undef PyRun_AnyFile
1847PyAPI_FUNC(int)
1848PyRun_AnyFile(FILE *fp, const char *name)
1849{
1850 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1851}
1852
1853#undef PyRun_AnyFileEx
1854PyAPI_FUNC(int)
1855PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1856{
1857 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1858}
1859
1860#undef PyRun_AnyFileFlags
1861PyAPI_FUNC(int)
1862PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1863{
1864 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1865}
1866
1867#undef PyRun_File
1868PyAPI_FUNC(PyObject *)
1869PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1870{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001871 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001872}
1873
1874#undef PyRun_FileEx
1875PyAPI_FUNC(PyObject *)
1876PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1877{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001878 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001879}
1880
1881#undef PyRun_FileFlags
1882PyAPI_FUNC(PyObject *)
1883PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1884 PyCompilerFlags *flags)
1885{
Georg Brandl3c0fd562008-07-05 10:07:18 +00001886 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001887}
1888
1889#undef PyRun_SimpleFile
1890PyAPI_FUNC(int)
1891PyRun_SimpleFile(FILE *f, const char *p)
1892{
1893 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1894}
1895
1896#undef PyRun_SimpleFileEx
1897PyAPI_FUNC(int)
1898PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1899{
1900 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1901}
1902
1903
1904#undef PyRun_String
1905PyAPI_FUNC(PyObject *)
1906PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1907{
1908 return PyRun_StringFlags(str, s, g, l, NULL);
1909}
1910
1911#undef PyRun_SimpleString
1912PyAPI_FUNC(int)
1913PyRun_SimpleString(const char *s)
1914{
1915 return PyRun_SimpleStringFlags(s, NULL);
1916}
1917
1918#undef Py_CompileString
1919PyAPI_FUNC(PyObject *)
1920Py_CompileString(const char *str, const char *p, int s)
1921{
1922 return Py_CompileStringFlags(str, p, s, NULL);
1923}
1924
1925#undef PyRun_InteractiveOne
1926PyAPI_FUNC(int)
1927PyRun_InteractiveOne(FILE *f, const char *p)
1928{
1929 return PyRun_InteractiveOneFlags(f, p, NULL);
1930}
1931
1932#undef PyRun_InteractiveLoop
1933PyAPI_FUNC(int)
1934PyRun_InteractiveLoop(FILE *f, const char *p)
1935{
1936 return PyRun_InteractiveLoopFlags(f, p, NULL);
1937}
1938
Anthony Baxterac6bd462006-04-13 02:06:09 +00001939#ifdef __cplusplus
1940}
1941#endif
1942