blob: ade5efee0c478338267cb10b47135d2c3a593972 [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000014#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000015#include "symtable.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000016#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "ast.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000018#include "eval.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000019#include "marshal.h"
Antoine Pitrouefb60c02009-10-20 21:29:37 +000020#include "abstract.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000021
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000022#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023#include <signal.h>
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000024#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000025
Benjamin Peterson796798b2009-01-02 20:47:27 +000026#ifdef MS_WINDOWS
Martin v. Löwis5344c992009-01-02 20:32:55 +000027#include "malloc.h" /* for alloca */
Benjamin Peterson796798b2009-01-02 20:47:27 +000028#endif
Martin v. Löwis5344c992009-01-02 20:32:55 +000029
Martin v. Löwis73d538b2003-03-05 15:13:47 +000030#ifdef HAVE_LANGINFO_H
31#include <locale.h>
32#include <langinfo.h>
33#endif
34
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000035#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000036#undef BYTE
37#include "windows.h"
38#endif
39
Neal Norwitz4281cef2006-03-04 19:58:13 +000040#ifndef Py_REF_DEBUG
Tim Peters62e97f02006-03-28 21:44:32 +000041#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000042#else /* Py_REF_DEBUG */
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043#define PRINT_TOTAL_REFS() fprintf(stderr, \
44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
45 _Py_GetRefTotal())
Neal Norwitz4281cef2006-03-04 19:58:13 +000046#endif
47
Anthony Baxterac6bd462006-04-13 02:06:09 +000048#ifdef __cplusplus
49extern "C" {
50#endif
51
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000052extern char *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000053
Guido van Rossum82598051997-03-05 00:20:32 +000054extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000055
Guido van Rossumb73cc041993-11-01 16:28:59 +000056/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000057static void initmain(void);
58static void initsite(void);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000060 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000061static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000063static void err_input(perrdetail *);
64static void initsigs(void);
Antoine Pitrouefb60c02009-10-20 21:29:37 +000065static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static void call_sys_exitfunc(void);
67static void call_ll_exitfuncs(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000068extern void _PyUnicode_Init(void);
69extern void _PyUnicode_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +000070
Mark Hammond8d98d2c2003-04-19 15:41:53 +000071#ifdef WITH_THREAD
72extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73extern void _PyGILState_Fini(void);
74#endif /* WITH_THREAD */
75
Guido van Rossum82598051997-03-05 00:20:32 +000076int Py_DebugFlag; /* Needed by parser.c */
77int Py_VerboseFlag; /* Needed by import.c */
Guido van Rossum7433b121997-02-14 19:45:36 +000078int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl11041f02011-05-15 08:50:32 +020079int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +000080int Py_NoSiteFlag; /* Suppress 'import site' */
Christian Heimes1a6387e2008-03-26 12:49:49 +000081int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Georg Brandl2da0fce2008-01-07 17:09:35 +000082int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +000083int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +000084int Py_FrozenFlag; /* Needed by getpath.c */
Guido van Rossumb16d1972000-05-01 17:55:15 +000085int Py_UnicodeFlag = 0; /* Needed by compile.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +000086int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Tim Peters3caca232001-12-06 06:23:26 +000087/* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
88 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
89 true divisions (which they will be in 2.3). */
90int _Py_QnewFlag = 0;
Christian Heimesaf748c32008-05-06 22:41:46 +000091int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Barry Warsaw1e13eb02012-02-20 20:42:21 -050092int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000093
Christian Heimes51c4d722013-10-22 10:22:29 +020094
95/* Hack to force loading of object files */
96int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
97 PyOS_mystrnicmp; /* Python/pystrcmp.o */
98
Brett Cannone9746892008-04-12 23:44:07 +000099/* PyModule_GetWarningsModule is no longer necessary as of 2.6
100since _warnings is builtin. This API should not be used. */
101PyObject *
102PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000103{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000104 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000105}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000106
Guido van Rossum25ce5661997-08-02 03:10:38 +0000107static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000108
Thomas Wouters7e474022000-07-16 12:04:32 +0000109/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000110
111int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000113{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000115}
116
Guido van Rossum25ce5661997-08-02 03:10:38 +0000117/* Global initializations. Can be undone by Py_Finalize(). Don't
118 call this twice without an intervening Py_Finalize() call. When
119 initializations fail, a fatal error is issued and the function does
120 not return. On return, the first thread and interpreter state have
121 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000122
Guido van Rossum25ce5661997-08-02 03:10:38 +0000123 Locking: you must hold the interpreter lock while calling this.
124 (If the lock has not yet been initialized, that's equivalent to
125 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000126
Guido van Rossum25ce5661997-08-02 03:10:38 +0000127*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000128
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000129static int
130add_flag(int flag, const char *envs)
131{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000132 int env = atoi(envs);
133 if (flag < env)
134 flag = env;
135 if (flag < 1)
136 flag = 1;
137 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000138}
139
Guido van Rossuma027efa1997-05-05 20:56:21 +0000140void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000141Py_InitializeEx(int install_sigs)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000142{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 PyInterpreterState *interp;
144 PyThreadState *tstate;
145 PyObject *bimod, *sysmod;
146 char *p;
147 char *icodeset = NULL; /* On Windows, input codeset may theoretically
148 differ from output codeset. */
149 char *codeset = NULL;
150 char *errors = NULL;
151 int free_codeset = 0;
152 int overridden = 0;
153 PyObject *sys_stream, *sys_isatty;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000154#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 char *saved_locale, *loc_codeset;
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000156#endif
Martin v. Löwis99815892008-06-01 07:20:46 +0000157#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000158 char ibuf[128];
159 char buf[128];
Martin v. Löwis99815892008-06-01 07:20:46 +0000160#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000162
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 if (initialized)
164 return;
165 initialized = 1;
Tim Petersd08e3822003-04-17 15:24:21 +0000166
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
168 Py_DebugFlag = add_flag(Py_DebugFlag, p);
169 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
170 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
171 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
172 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
173 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
174 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Barry Warsaw1e13eb02012-02-20 20:42:21 -0500175 /* The variable is only tested for existence here; _PyRandom_Init will
176 check its value further. */
177 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
178 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
179
180 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000181
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000182 interp = PyInterpreterState_New();
183 if (interp == NULL)
184 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000185
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 tstate = PyThreadState_New(interp);
187 if (tstate == NULL)
188 Py_FatalError("Py_Initialize: can't make first thread");
189 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000190
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000192
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 if (!_PyFrame_Init())
194 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000195
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 if (!_PyInt_Init())
197 Py_FatalError("Py_Initialize: can't init ints");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000198
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 if (!_PyLong_Init())
200 Py_FatalError("Py_Initialize: can't init longs");
Mark Dickinsonefc82f72009-03-20 15:51:55 +0000201
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 if (!PyByteArray_Init())
203 Py_FatalError("Py_Initialize: can't init bytearray");
Christian Heimes1a6387e2008-03-26 12:49:49 +0000204
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 _PyFloat_Init();
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000206
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000207 interp->modules = PyDict_New();
208 if (interp->modules == NULL)
209 Py_FatalError("Py_Initialize: can't make modules dictionary");
210 interp->modules_reloading = PyDict_New();
211 if (interp->modules_reloading == NULL)
212 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000213
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000214#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 /* Init Unicode implementation; relies on the codec registry */
216 _PyUnicode_Init();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000217#endif
Guido van Rossumc94044c2000-03-10 23:03:54 +0000218
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 bimod = _PyBuiltin_Init();
220 if (bimod == NULL)
221 Py_FatalError("Py_Initialize: can't initialize __builtin__");
222 interp->builtins = PyModule_GetDict(bimod);
223 if (interp->builtins == NULL)
224 Py_FatalError("Py_Initialize: can't initialize builtins dict");
225 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 sysmod = _PySys_Init();
228 if (sysmod == NULL)
229 Py_FatalError("Py_Initialize: can't initialize sys");
230 interp->sysdict = PyModule_GetDict(sysmod);
231 if (interp->sysdict == NULL)
232 Py_FatalError("Py_Initialize: can't initialize sys dict");
233 Py_INCREF(interp->sysdict);
234 _PyImport_FixupExtension("sys", "sys");
235 PySys_SetPath(Py_GetPath());
236 PyDict_SetItemString(interp->sysdict, "modules",
237 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000238
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000240
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 /* initialize builtin exceptions */
242 _PyExc_Init();
243 _PyImport_FixupExtension("exceptions", "exceptions");
Barry Warsawf242aa02000-05-25 23:09:49 +0000244
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 /* phase 2 of builtins */
246 _PyImport_FixupExtension("__builtin__", "__builtin__");
Barry Warsaw035574d1997-08-29 22:07:17 +0000247
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000248 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000249
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 if (install_sigs)
251 initsigs(); /* Signal handling stuff, including initintr() */
Brett Cannonc33e82d2010-05-05 20:38:52 +0000252
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000253 /* Initialize warnings. */
254 _PyWarnings_Init();
255 if (PySys_HasWarnOptions()) {
256 PyObject *warnings_module = PyImport_ImportModule("warnings");
257 if (!warnings_module)
258 PyErr_Clear();
259 Py_XDECREF(warnings_module);
260 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000261
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000262 initmain(); /* Module __main__ */
Just van Rossum5bfba3a2003-02-25 20:25:12 +0000263
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000264 /* auto-thread-state API, if available */
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000265#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 _PyGILState_Init(interp, tstate);
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000267#endif /* WITH_THREAD */
268
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 if (!Py_NoSiteFlag)
270 initsite(); /* Module site */
Victor Stinner66644262010-03-10 22:30:19 +0000271
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000272 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
273 p = icodeset = codeset = strdup(p);
274 free_codeset = 1;
275 errors = strchr(p, ':');
276 if (errors) {
277 *errors = '\0';
278 errors++;
279 }
280 overridden = 1;
281 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000282
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000283#if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 /* On Unix, set the file system encoding according to the
285 user's preference, if the CODESET names a well-known
286 Python codec, and Py_FileSystemDefaultEncoding isn't
287 initialized by other means. Also set the encoding of
288 stdin and stdout if these are terminals, unless overridden. */
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000289
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000290 if (!overridden || !Py_FileSystemDefaultEncoding) {
291 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
292 setlocale(LC_CTYPE, "");
293 loc_codeset = nl_langinfo(CODESET);
294 if (loc_codeset && *loc_codeset) {
295 PyObject *enc = PyCodec_Encoder(loc_codeset);
296 if (enc) {
297 loc_codeset = strdup(loc_codeset);
298 Py_DECREF(enc);
299 } else {
300 if (PyErr_ExceptionMatches(PyExc_LookupError)) {
301 PyErr_Clear();
302 loc_codeset = NULL;
303 } else {
304 PyErr_Print();
305 exit(1);
306 }
307 }
308 } else
309 loc_codeset = NULL;
310 setlocale(LC_CTYPE, saved_locale);
311 free(saved_locale);
Martin v. Löwis99815892008-06-01 07:20:46 +0000312
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 if (!overridden) {
314 codeset = icodeset = loc_codeset;
315 free_codeset = 1;
316 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000317
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000318 /* Initialize Py_FileSystemDefaultEncoding from
319 locale even if PYTHONIOENCODING is set. */
320 if (!Py_FileSystemDefaultEncoding) {
321 Py_FileSystemDefaultEncoding = loc_codeset;
322 if (!overridden)
323 free_codeset = 0;
324 }
325 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000326#endif
327
328#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000329 if (!overridden) {
330 icodeset = ibuf;
331 codeset = buf;
332 sprintf(ibuf, "cp%d", GetConsoleCP());
333 sprintf(buf, "cp%d", GetConsoleOutputCP());
334 }
Martin v. Löwis99815892008-06-01 07:20:46 +0000335#endif
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000336
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 if (codeset) {
338 sys_stream = PySys_GetObject("stdin");
339 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
340 if (!sys_isatty)
341 PyErr_Clear();
342 if ((overridden ||
343 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
344 PyFile_Check(sys_stream)) {
345 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
346 Py_FatalError("Cannot set codeset of stdin");
347 }
348 Py_XDECREF(sys_isatty);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000349
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 sys_stream = PySys_GetObject("stdout");
351 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
352 if (!sys_isatty)
353 PyErr_Clear();
354 if ((overridden ||
355 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
356 PyFile_Check(sys_stream)) {
357 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
358 Py_FatalError("Cannot set codeset of stdout");
359 }
360 Py_XDECREF(sys_isatty);
Martin v. Löwisa2c17c52003-08-09 09:47:11 +0000361
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 sys_stream = PySys_GetObject("stderr");
363 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
364 if (!sys_isatty)
365 PyErr_Clear();
366 if((overridden ||
367 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
368 PyFile_Check(sys_stream)) {
369 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
370 Py_FatalError("Cannot set codeset of stderr");
371 }
372 Py_XDECREF(sys_isatty);
Martin v. Löwisea62d252006-04-03 10:56:49 +0000373
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 if (free_codeset)
375 free(codeset);
376 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000377}
378
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000379void
380Py_Initialize(void)
381{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000383}
384
385
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000386#ifdef COUNT_ALLOCS
Martin v. Löwis45294a92006-04-18 06:24:08 +0000387extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000388#endif
389
Guido van Rossum25ce5661997-08-02 03:10:38 +0000390/* Undo the effect of Py_Initialize().
391
392 Beware: if multiple interpreter and/or thread states exist, these
393 are not wiped out; only the current thread and interpreter state
394 are deleted. But since everything else is deleted, those other
395 interpreter and thread states should no longer be used.
396
397 (XXX We should do better, e.g. wipe out all interpreters and
398 threads.)
399
400 Locking: as above.
401
402*/
403
404void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000405Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000406{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 PyInterpreterState *interp;
408 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000409
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 if (!initialized)
411 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000412
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000413 wait_for_thread_shutdown();
Antoine Pitrouefb60c02009-10-20 21:29:37 +0000414
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 /* The interpreter is still entirely intact at this point, and the
416 * exit funcs may be relying on that. In particular, if some thread
417 * or exit func is still waiting to do an import, the import machinery
418 * expects Py_IsInitialized() to return true. So don't say the
419 * interpreter is uninitialized until after the exit funcs have run.
420 * Note that Threading.py uses an exit func to do a join on all the
421 * threads created thru it, so this also protects pending imports in
422 * the threads created via Threading.
423 */
424 call_sys_exitfunc();
Antoine Pitroub9a45012014-11-21 02:04:21 +0100425 initialized = 0;
Guido van Rossum4cc462e1998-01-19 22:00:38 +0000426
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000427 /* Get current thread state and interpreter pointer */
428 tstate = PyThreadState_GET();
429 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000430
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 /* Disable signal handling */
432 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000433
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 /* Clear type lookup cache */
435 PyType_ClearCache();
Christian Heimes908caac2008-01-27 23:34:59 +0000436
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000437 /* Collect garbage. This may call finalizers; it's nice to call these
438 * before all modules are destroyed.
439 * XXX If a __del__ or weakref callback is triggered here, and tries to
440 * XXX import a module, bad things can happen, because Python no
441 * XXX longer believes it's initialized.
442 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
443 * XXX is easy to provoke that way. I've also seen, e.g.,
444 * XXX Exception exceptions.ImportError: 'No module named sha'
445 * XXX in <function callback at 0x008F5718> ignored
446 * XXX but I'm unclear on exactly how that one happens. In any case,
447 * XXX I haven't seen a real-life report of either of these.
448 */
449 PyGC_Collect();
Martin v. Löwis45294a92006-04-18 06:24:08 +0000450#ifdef COUNT_ALLOCS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 /* With COUNT_ALLOCS, it helps to run GC multiple times:
452 each collection might release some types from the type
453 list, so they become garbage. */
454 while (PyGC_Collect() > 0)
455 /* nothing */;
Martin v. Löwis45294a92006-04-18 06:24:08 +0000456#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 /* Destroy all modules */
459 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 /* Collect final garbage. This disposes of cycles created by
462 * new-style class definitions, for example.
463 * XXX This is disabled because it caused too many problems. If
464 * XXX a __del__ or weakref callback triggers here, Python code has
465 * XXX a hard time running, because even the sys module has been
466 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
467 * XXX One symptom is a sequence of information-free messages
468 * XXX coming from threads (if a __del__ or callback is invoked,
469 * XXX other threads can execute too, and any exception they encounter
470 * XXX triggers a comedy of errors as subsystem after subsystem
471 * XXX fails to find what it *expects* to find in sys to help report
472 * XXX the exception and consequent unexpected failures). I've also
473 * XXX seen segfaults then, after adding print statements to the
474 * XXX Python code getting called.
475 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000476#if 0
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000477 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000478#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000479
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
481 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000482
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000484#ifdef COUNT_ALLOCS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000486#endif
487
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000489
Tim Peters9cf25ce2003-04-17 15:21:01 +0000490#ifdef Py_TRACE_REFS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 /* Display all objects still alive -- this can invoke arbitrary
492 * __repr__ overrides, so requires a mostly-intact interpreter.
493 * Alas, a lot of stuff may still be alive now that will be cleaned
494 * up later.
495 */
496 if (Py_GETENV("PYTHONDUMPREFS"))
497 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000498#endif /* Py_TRACE_REFS */
499
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000500 /* Clear interpreter state */
501 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000502
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000503 /* Now we decref the exception classes. After this point nothing
504 can raise an exception. That's okay, because each Fini() method
505 below has been checked to make sure no exceptions are ever
506 raised.
507 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000508
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000509 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000510
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 /* Cleanup auto-thread-state */
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000512#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000513 _PyGILState_Fini();
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000514#endif /* WITH_THREAD */
515
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 /* Delete current thread */
517 PyThreadState_Swap(NULL);
518 PyInterpreterState_Delete(interp);
Barry Warsawf242aa02000-05-25 23:09:49 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 /* Sundry finalizers */
521 PyMethod_Fini();
522 PyFrame_Fini();
523 PyCFunction_Fini();
524 PyTuple_Fini();
525 PyList_Fini();
526 PySet_Fini();
527 PyString_Fini();
528 PyByteArray_Fini();
529 PyInt_Fini();
530 PyFloat_Fini();
531 PyDict_Fini();
Benjamin Peterson57057a62014-08-28 12:30:00 -0400532 _PyRandom_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000533
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000534#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 /* Cleanup Unicode implementation */
536 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000537#endif
538
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000539 /* XXX Still allocated:
540 - various static ad-hoc pointers to interned strings
541 - int and float free list blocks
542 - whatever various modules and libraries allocate
543 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000544
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000546
Tim Peters269b2a62003-04-17 19:52:29 +0000547#ifdef Py_TRACE_REFS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 /* Display addresses (& refcnts) of all objects still alive.
549 * An address can be used to find the repr of the object, printed
550 * above by _Py_PrintReferences.
551 */
552 if (Py_GETENV("PYTHONDUMPREFS"))
553 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000554#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000555#ifdef PYMALLOC_DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000556 if (Py_GETENV("PYTHONMALLOCSTATS"))
557 _PyObject_DebugMallocStats();
Tim Peters0e871182002-04-13 08:29:14 +0000558#endif
559
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000560 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000561}
562
563/* Create and initialize a new interpreter and thread, and return the
564 new thread. This requires that Py_Initialize() has been called
565 first.
566
567 Unsuccessful initialization yields a NULL pointer. Note that *no*
568 exception information is available even in this case -- the
569 exception information is held in the thread, and there is no
570 thread.
571
572 Locking: as above.
573
574*/
575
576PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000577Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000578{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000579 PyInterpreterState *interp;
580 PyThreadState *tstate, *save_tstate;
581 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000582
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000583 if (!initialized)
584 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000585
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000586 interp = PyInterpreterState_New();
587 if (interp == NULL)
588 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000589
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000590 tstate = PyThreadState_New(interp);
591 if (tstate == NULL) {
592 PyInterpreterState_Delete(interp);
593 return NULL;
594 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000595
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000596 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000597
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000598 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000599
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000600 interp->modules = PyDict_New();
601 interp->modules_reloading = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000602
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000603 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
604 if (bimod != NULL) {
605 interp->builtins = PyModule_GetDict(bimod);
606 if (interp->builtins == NULL)
607 goto handle_error;
608 Py_INCREF(interp->builtins);
609 }
610 sysmod = _PyImport_FindExtension("sys", "sys");
611 if (bimod != NULL && sysmod != NULL) {
612 interp->sysdict = PyModule_GetDict(sysmod);
613 if (interp->sysdict == NULL)
614 goto handle_error;
615 Py_INCREF(interp->sysdict);
616 PySys_SetPath(Py_GetPath());
617 PyDict_SetItemString(interp->sysdict, "modules",
618 interp->modules);
619 _PyImportHooks_Init();
620 initmain();
621 if (!Py_NoSiteFlag)
622 initsite();
623 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000624
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000625 if (!PyErr_Occurred())
626 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000627
Neal Norwitz0c6ae5b2006-08-21 20:16:24 +0000628handle_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000630
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 PyErr_Print();
632 PyThreadState_Clear(tstate);
633 PyThreadState_Swap(save_tstate);
634 PyThreadState_Delete(tstate);
635 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000636
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000638}
639
640/* Delete an interpreter and its last thread. This requires that the
641 given thread state is current, that the thread has no remaining
642 frames, and that it is its interpreter's only remaining thread.
643 It is a fatal error to violate these constraints.
644
645 (Py_Finalize() doesn't have these constraints -- it zaps
646 everything, regardless.)
647
648 Locking: as above.
649
650*/
651
652void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000653Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000654{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000655 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000656
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 if (tstate != PyThreadState_GET())
658 Py_FatalError("Py_EndInterpreter: thread is not current");
659 if (tstate->frame != NULL)
660 Py_FatalError("Py_EndInterpreter: thread still has a frame");
661 if (tstate != interp->tstate_head || tstate->next != NULL)
662 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 PyImport_Cleanup();
665 PyInterpreterState_Clear(interp);
666 PyThreadState_Swap(NULL);
667 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000668}
669
670static char *progname = "python";
671
672void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000673Py_SetProgramName(char *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000674{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000675 if (pn && *pn)
676 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000677}
678
679char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000680Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000681{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000682 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000683}
684
Guido van Rossuma61691e1998-02-06 22:27:24 +0000685static char *default_home = NULL;
686
687void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000688Py_SetPythonHome(char *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000689{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000691}
692
693char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000694Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000695{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 char *home = default_home;
697 if (home == NULL && !Py_IgnoreEnvironmentFlag)
698 home = Py_GETENV("PYTHONHOME");
699 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000700}
701
Guido van Rossum6135a871995-01-09 17:53:26 +0000702/* Create __main__ module */
703
704static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000705initmain(void)
Guido van Rossum6135a871995-01-09 17:53:26 +0000706{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 PyObject *m, *d;
708 m = PyImport_AddModule("__main__");
709 if (m == NULL)
710 Py_FatalError("can't create __main__ module");
711 d = PyModule_GetDict(m);
712 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
713 PyObject *bimod = PyImport_ImportModule("__builtin__");
714 if (bimod == NULL ||
715 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
716 Py_FatalError("can't add __builtins__ to __main__");
717 Py_XDECREF(bimod);
718 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000719}
720
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000721/* Import the site module (not into __main__ though) */
722
723static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000724initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000725{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000726 PyObject *m;
727 m = PyImport_ImportModule("site");
728 if (m == NULL) {
729 PyErr_Print();
730 Py_Finalize();
731 exit(1);
732 }
733 else {
734 Py_DECREF(m);
735 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000736}
737
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000738/* Parse input from a file and execute it */
739
740int
Tim Peters5e9d6cf2006-05-28 10:41:29 +0000741PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000743{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 if (filename == NULL)
745 filename = "???";
746 if (Py_FdIsInteractive(fp, filename)) {
747 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
748 if (closeit)
749 fclose(fp);
750 return err;
751 }
752 else
753 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000754}
755
756int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000757PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000758{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000759 PyObject *v;
760 int ret;
761 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000762
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000763 if (flags == NULL) {
764 flags = &local_flags;
765 local_flags.cf_flags = 0;
766 }
767 v = PySys_GetObject("ps1");
768 if (v == NULL) {
769 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
770 Py_XDECREF(v);
771 }
772 v = PySys_GetObject("ps2");
773 if (v == NULL) {
774 PySys_SetObject("ps2", v = PyString_FromString("... "));
775 Py_XDECREF(v);
776 }
777 for (;;) {
778 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
779 PRINT_TOTAL_REFS();
780 if (ret == E_EOF)
781 return 0;
782 /*
783 if (ret == E_NOMEM)
784 return -1;
785 */
786 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000787}
788
Eric Smith7c478942008-03-18 23:45:49 +0000789#if 0
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000790/* compute parser flags based on compiler flags */
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000791#define PARSER_FLAGS(flags) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000792 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
793 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
Eric Smith7c478942008-03-18 23:45:49 +0000794#endif
795#if 1
Neal Norwitzca460d92006-09-06 06:28:06 +0000796/* Keep an example of flags with future keyword support. */
797#define PARSER_FLAGS(flags) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000798 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
799 PyPARSE_DONT_IMPLY_DEDENT : 0) \
800 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
801 PyPARSE_PRINT_IS_FUNCTION : 0) \
802 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
803 PyPARSE_UNICODE_LITERALS : 0) \
804 ) : 0)
Neal Norwitzca460d92006-09-06 06:28:06 +0000805#endif
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000806
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000807int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000808PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000809{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000810 PyObject *m, *d, *v, *w;
811 mod_ty mod;
812 PyArena *arena;
813 char *ps1 = "", *ps2 = "";
814 int errcode = 0;
Tim Petersfe2127d2001-07-16 05:37:24 +0000815
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000816 v = PySys_GetObject("ps1");
817 if (v != NULL) {
818 v = PyObject_Str(v);
819 if (v == NULL)
820 PyErr_Clear();
821 else if (PyString_Check(v))
822 ps1 = PyString_AsString(v);
823 }
824 w = PySys_GetObject("ps2");
825 if (w != NULL) {
826 w = PyObject_Str(w);
827 if (w == NULL)
828 PyErr_Clear();
829 else if (PyString_Check(w))
830 ps2 = PyString_AsString(w);
831 }
832 arena = PyArena_New();
833 if (arena == NULL) {
834 Py_XDECREF(v);
835 Py_XDECREF(w);
836 return -1;
837 }
838 mod = PyParser_ASTFromFile(fp, filename,
839 Py_single_input, ps1, ps2,
840 flags, &errcode, arena);
841 Py_XDECREF(v);
842 Py_XDECREF(w);
843 if (mod == NULL) {
844 PyArena_Free(arena);
845 if (errcode == E_EOF) {
846 PyErr_Clear();
847 return E_EOF;
848 }
849 PyErr_Print();
850 return -1;
851 }
852 m = PyImport_AddModule("__main__");
853 if (m == NULL) {
854 PyArena_Free(arena);
855 return -1;
856 }
857 d = PyModule_GetDict(m);
858 v = run_mod(mod, filename, d, d, flags, arena);
859 PyArena_Free(arena);
860 if (v == NULL) {
861 PyErr_Print();
862 return -1;
863 }
864 Py_DECREF(v);
865 if (Py_FlushLine())
866 PyErr_Clear();
867 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000868}
869
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000870/* Check whether a file maybe a pyc file: Look at the extension,
871 the file type, and, if we may close it, at the first few bytes. */
872
873static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000874maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000875{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
877 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 /* Only look into the file if we are allowed to close it, since
880 it then should also be seekable. */
881 if (closeit) {
882 /* Read only two bytes of the magic. If the file was opened in
883 text mode, the bytes 3 and 4 of the magic (\r\n) might not
884 be read as they are on disk. */
885 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
886 unsigned char buf[2];
887 /* Mess: In case of -x, the stream is NOT at its start now,
888 and ungetc() was used to push back the first newline,
889 which makes the current stream position formally undefined,
890 and a x-platform nightmare.
891 Unfortunately, we have no direct way to know whether -x
892 was specified. So we use a terrible hack: if the current
893 stream position is not 0, we assume -x was specified, and
894 give up. Bug 132850 on SourceForge spells out the
895 hopelessness of trying anything else (fseek and ftell
896 don't work predictably x-platform for text-mode files).
897 */
898 int ispyc = 0;
899 if (ftell(fp) == 0) {
900 if (fread(buf, 1, 2, fp) == 2 &&
901 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
902 ispyc = 1;
903 rewind(fp);
904 }
905 return ispyc;
906 }
907 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +0000908}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +0000909
Guido van Rossum0df002c2000-08-27 19:21:52 +0000910int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000911PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000912 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +0000913{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000914 PyObject *m, *d, *v;
915 const char *ext;
Hynek Schlawackb271b3e2012-11-07 09:41:28 +0100916 int set_file_name = 0, len, ret = -1;
Guido van Rossumfdef2711994-09-14 13:31:04 +0000917
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000918 m = PyImport_AddModule("__main__");
919 if (m == NULL)
920 return -1;
Hynek Schlawackb271b3e2012-11-07 09:41:28 +0100921 Py_INCREF(m);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 d = PyModule_GetDict(m);
923 if (PyDict_GetItemString(d, "__file__") == NULL) {
924 PyObject *f = PyString_FromString(filename);
925 if (f == NULL)
Hynek Schlawackb271b3e2012-11-07 09:41:28 +0100926 goto done;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 if (PyDict_SetItemString(d, "__file__", f) < 0) {
928 Py_DECREF(f);
Hynek Schlawackb271b3e2012-11-07 09:41:28 +0100929 goto done;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 }
931 set_file_name = 1;
932 Py_DECREF(f);
933 }
934 len = strlen(filename);
935 ext = filename + len - (len > 4 ? 4 : 0);
936 if (maybe_pyc_file(fp, filename, ext, closeit)) {
937 /* Try to run a pyc file. First, re-open in binary */
938 if (closeit)
939 fclose(fp);
940 if ((fp = fopen(filename, "rb")) == NULL) {
941 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000942 goto done;
943 }
944 /* Turn on optimization if a .pyo file is given */
945 if (strcmp(ext, ".pyo") == 0)
946 Py_OptimizeFlag = 1;
947 v = run_pyc_file(fp, filename, d, d, flags);
948 } else {
949 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
950 closeit, flags);
951 }
952 if (v == NULL) {
953 PyErr_Print();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 goto done;
955 }
956 Py_DECREF(v);
957 if (Py_FlushLine())
958 PyErr_Clear();
959 ret = 0;
Georg Brandlaa2321b2007-03-07 00:40:28 +0000960 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961 if (set_file_name && PyDict_DelItemString(d, "__file__"))
962 PyErr_Clear();
Hynek Schlawackb271b3e2012-11-07 09:41:28 +0100963 Py_DECREF(m);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000965}
966
967int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000968PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +0000969{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000970 PyObject *m, *d, *v;
971 m = PyImport_AddModule("__main__");
972 if (m == NULL)
973 return -1;
974 d = PyModule_GetDict(m);
975 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
976 if (v == NULL) {
977 PyErr_Print();
978 return -1;
979 }
980 Py_DECREF(v);
981 if (Py_FlushLine())
982 PyErr_Clear();
983 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000984}
985
Barry Warsaw035574d1997-08-29 22:07:17 +0000986static int
Martin v. Löwis95292d62002-12-11 14:04:59 +0000987parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000988 int *lineno, int *offset, const char **text)
Barry Warsaw035574d1997-08-29 22:07:17 +0000989{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000990 long hold;
991 PyObject *v;
Barry Warsaw035574d1997-08-29 22:07:17 +0000992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 /* old style errors */
994 if (PyTuple_Check(err))
995 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
996 lineno, offset, text);
Barry Warsaw035574d1997-08-29 22:07:17 +0000997
Benjamin Petersonb9348e72012-04-03 00:30:38 -0400998 *message = NULL;
999
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001000 /* new style errors. `err' is an instance */
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001001 *message = PyObject_GetAttrString(err, "msg");
1002 if (!*message)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001004
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001005 v = PyObject_GetAttrString(err, "filename");
1006 if (!v)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001007 goto finally;
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001008 if (v == Py_None) {
1009 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001010 *filename = NULL;
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001011 }
1012 else {
1013 *filename = PyString_AsString(v);
1014 Py_DECREF(v);
1015 if (!*filename)
1016 goto finally;
1017 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001018
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001019 v = PyObject_GetAttrString(err, "lineno");
1020 if (!v)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001021 goto finally;
1022 hold = PyInt_AsLong(v);
1023 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 if (hold < 0 && PyErr_Occurred())
1025 goto finally;
1026 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001027
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001028 v = PyObject_GetAttrString(err, "offset");
1029 if (!v)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001030 goto finally;
1031 if (v == Py_None) {
1032 *offset = -1;
1033 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001034 } else {
1035 hold = PyInt_AsLong(v);
1036 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001037 if (hold < 0 && PyErr_Occurred())
1038 goto finally;
1039 *offset = (int)hold;
1040 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001041
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001042 v = PyObject_GetAttrString(err, "text");
1043 if (!v)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001044 goto finally;
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001045 if (v == Py_None) {
1046 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 *text = NULL;
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001048 }
1049 else {
1050 *text = PyString_AsString(v);
1051 Py_DECREF(v);
1052 if (!*text)
1053 goto finally;
1054 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001055 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001056
1057finally:
Benjamin Petersonb9348e72012-04-03 00:30:38 -04001058 Py_XDECREF(*message);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001059 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001060}
1061
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001062void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001063PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001064{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001065 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001066}
1067
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001068static void
Martin v. Löwis95292d62002-12-11 14:04:59 +00001069print_error_text(PyObject *f, int offset, const char *text)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001070{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001071 char *nl;
1072 if (offset >= 0) {
Benjamin Petersonec9f9f52010-10-29 03:45:34 +00001073 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1074 offset--;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 for (;;) {
1076 nl = strchr(text, '\n');
1077 if (nl == NULL || nl-text >= offset)
1078 break;
1079 offset -= (int)(nl+1-text);
1080 text = nl+1;
1081 }
1082 while (*text == ' ' || *text == '\t') {
1083 text++;
1084 offset--;
1085 }
1086 }
1087 PyFile_WriteString(" ", f);
1088 PyFile_WriteString(text, f);
1089 if (*text == '\0' || text[strlen(text)-1] != '\n')
1090 PyFile_WriteString("\n", f);
1091 if (offset == -1)
1092 return;
1093 PyFile_WriteString(" ", f);
1094 offset--;
1095 while (offset > 0) {
1096 PyFile_WriteString(" ", f);
1097 offset--;
1098 }
1099 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001100}
1101
Guido van Rossum66e8e862001-03-23 17:54:43 +00001102static void
1103handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001104{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001105 PyObject *exception, *value, *tb;
1106 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001108 if (Py_InspectFlag)
1109 /* Don't exit if -i flag was given. This flag is set to 0
1110 * when entering interactive mode for inspecting. */
1111 return;
Georg Brandl49aafc92007-03-07 00:34:46 +00001112
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001113 PyErr_Fetch(&exception, &value, &tb);
1114 if (Py_FlushLine())
1115 PyErr_Clear();
1116 fflush(stdout);
1117 if (value == NULL || value == Py_None)
1118 goto done;
1119 if (PyExceptionInstance_Check(value)) {
1120 /* The error code should be in the `code' attribute. */
1121 PyObject *code = PyObject_GetAttrString(value, "code");
1122 if (code) {
1123 Py_DECREF(value);
1124 value = code;
1125 if (value == Py_None)
1126 goto done;
1127 }
1128 /* If we failed to dig out the 'code' attribute,
1129 just let the else clause below print the error. */
1130 }
1131 if (PyInt_Check(value))
1132 exitcode = (int)PyInt_AsLong(value);
1133 else {
Victor Stinnerc49dfcc2010-05-25 22:30:32 +00001134 PyObject *sys_stderr = PySys_GetObject("stderr");
1135 if (sys_stderr != NULL && sys_stderr != Py_None) {
1136 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1137 } else {
1138 PyObject_Print(value, stderr, Py_PRINT_RAW);
1139 fflush(stderr);
1140 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001141 PySys_WriteStderr("\n");
1142 exitcode = 1;
1143 }
Tim Peterscf615b52003-04-19 18:47:02 +00001144 done:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001145 /* Restore and clear the exception info, in order to properly decref
1146 * the exception, value, and traceback. If we just exit instead,
1147 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1148 * some finalizers from running.
1149 */
1150 PyErr_Restore(exception, value, tb);
1151 PyErr_Clear();
1152 Py_Exit(exitcode);
1153 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001154}
1155
1156void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001157PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001159 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001161 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1162 handle_system_exit();
1163 }
1164 PyErr_Fetch(&exception, &v, &tb);
1165 if (exception == NULL)
1166 return;
1167 PyErr_NormalizeException(&exception, &v, &tb);
1168 if (exception == NULL)
1169 return;
1170 /* Now we know v != NULL too */
1171 if (set_sys_last_vars) {
1172 PySys_SetObject("last_type", exception);
1173 PySys_SetObject("last_value", v);
1174 PySys_SetObject("last_traceback", tb);
1175 }
1176 hook = PySys_GetObject("excepthook");
Antoine Pitroua03ff6d2010-08-08 21:37:51 +00001177 if (hook && hook != Py_None) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001178 PyObject *args = PyTuple_Pack(3,
1179 exception, v, tb ? tb : Py_None);
1180 PyObject *result = PyEval_CallObject(hook, args);
1181 if (result == NULL) {
1182 PyObject *exception2, *v2, *tb2;
1183 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1184 handle_system_exit();
1185 }
1186 PyErr_Fetch(&exception2, &v2, &tb2);
1187 PyErr_NormalizeException(&exception2, &v2, &tb2);
1188 /* It should not be possible for exception2 or v2
1189 to be NULL. However PyErr_Display() can't
1190 tolerate NULLs, so just be safe. */
1191 if (exception2 == NULL) {
1192 exception2 = Py_None;
1193 Py_INCREF(exception2);
1194 }
1195 if (v2 == NULL) {
1196 v2 = Py_None;
1197 Py_INCREF(v2);
1198 }
1199 if (Py_FlushLine())
1200 PyErr_Clear();
1201 fflush(stdout);
1202 PySys_WriteStderr("Error in sys.excepthook:\n");
1203 PyErr_Display(exception2, v2, tb2);
1204 PySys_WriteStderr("\nOriginal exception was:\n");
1205 PyErr_Display(exception, v, tb);
1206 Py_DECREF(exception2);
1207 Py_DECREF(v2);
1208 Py_XDECREF(tb2);
1209 }
1210 Py_XDECREF(result);
1211 Py_XDECREF(args);
1212 } else {
1213 PySys_WriteStderr("sys.excepthook is missing\n");
1214 PyErr_Display(exception, v, tb);
1215 }
1216 Py_XDECREF(exception);
1217 Py_XDECREF(v);
1218 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001219}
1220
Richard Jones7b9558d2006-05-27 12:29:24 +00001221void
1222PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001223{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001224 int err = 0;
1225 PyObject *f = PySys_GetObject("stderr");
1226 Py_INCREF(value);
Antoine Pitroua03ff6d2010-08-08 21:37:51 +00001227 if (f == NULL || f == Py_None)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001228 fprintf(stderr, "lost sys.stderr\n");
1229 else {
1230 if (Py_FlushLine())
1231 PyErr_Clear();
1232 fflush(stdout);
1233 if (tb && tb != Py_None)
1234 err = PyTraceBack_Print(tb, f);
1235 if (err == 0 &&
1236 PyObject_HasAttrString(value, "print_file_and_line"))
1237 {
1238 PyObject *message;
1239 const char *filename, *text;
1240 int lineno, offset;
1241 if (!parse_syntax_error(value, &message, &filename,
1242 &lineno, &offset, &text))
1243 PyErr_Clear();
1244 else {
1245 char buf[10];
1246 PyFile_WriteString(" File \"", f);
1247 if (filename == NULL)
1248 PyFile_WriteString("<string>", f);
1249 else
1250 PyFile_WriteString(filename, f);
1251 PyFile_WriteString("\", line ", f);
1252 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1253 PyFile_WriteString(buf, f);
1254 PyFile_WriteString("\n", f);
1255 if (text != NULL)
1256 print_error_text(f, offset, text);
1257 Py_DECREF(value);
1258 value = message;
1259 /* Can't be bothered to check all those
1260 PyFile_WriteString() calls */
1261 if (PyErr_Occurred())
1262 err = -1;
1263 }
1264 }
1265 if (err) {
1266 /* Don't do anything else */
1267 }
1268 else if (PyExceptionClass_Check(exception)) {
1269 PyObject* moduleName;
1270 char* className = PyExceptionClass_Name(exception);
1271 if (className != NULL) {
1272 char *dot = strrchr(className, '.');
1273 if (dot != NULL)
1274 className = dot+1;
1275 }
Barry Warsaw2f5f6a21997-09-16 21:42:03 +00001276
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001277 moduleName = PyObject_GetAttrString(exception, "__module__");
1278 if (moduleName == NULL)
1279 err = PyFile_WriteString("<unknown>", f);
1280 else {
1281 char* modstr = PyString_AsString(moduleName);
1282 if (modstr && strcmp(modstr, "exceptions"))
1283 {
1284 err = PyFile_WriteString(modstr, f);
1285 err += PyFile_WriteString(".", f);
1286 }
1287 Py_DECREF(moduleName);
1288 }
1289 if (err == 0) {
1290 if (className == NULL)
1291 err = PyFile_WriteString("<unknown>", f);
1292 else
1293 err = PyFile_WriteString(className, f);
1294 }
1295 }
1296 else
1297 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1298 if (err == 0 && (value != Py_None)) {
1299 PyObject *s = PyObject_Str(value);
1300 /* only print colon if the str() of the
1301 object is not the empty string
1302 */
1303 if (s == NULL)
1304 err = -1;
1305 else if (!PyString_Check(s) ||
1306 PyString_GET_SIZE(s) != 0)
1307 err = PyFile_WriteString(": ", f);
1308 if (err == 0)
1309 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1310 Py_XDECREF(s);
1311 }
1312 /* try to write a newline in any case */
1313 err += PyFile_WriteString("\n", f);
1314 }
1315 Py_DECREF(value);
1316 /* If an error happened here, don't show it.
1317 XXX This is wrong, but too many callers rely on this behavior. */
1318 if (err != 0)
1319 PyErr_Clear();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001320}
1321
Guido van Rossum82598051997-03-05 00:20:32 +00001322PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001323PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001324 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001325{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001326 PyObject *ret = NULL;
1327 mod_ty mod;
1328 PyArena *arena = PyArena_New();
1329 if (arena == NULL)
1330 return NULL;
Brett Cannonc33e82d2010-05-05 20:38:52 +00001331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001332 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1333 if (mod != NULL)
1334 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1335 PyArena_Free(arena);
1336 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001337}
1338
1339PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001340PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001341 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001342{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001343 PyObject *ret;
1344 mod_ty mod;
1345 PyArena *arena = PyArena_New();
1346 if (arena == NULL)
1347 return NULL;
Brett Cannonc33e82d2010-05-05 20:38:52 +00001348
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001349 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1350 flags, NULL, arena);
1351 if (closeit)
1352 fclose(fp);
1353 if (mod == NULL) {
1354 PyArena_Free(arena);
1355 return NULL;
1356 }
1357 ret = run_mod(mod, filename, globals, locals, flags, arena);
1358 PyArena_Free(arena);
1359 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001360}
1361
Guido van Rossum82598051997-03-05 00:20:32 +00001362static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001364 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001365{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001366 PyCodeObject *co;
1367 PyObject *v;
1368 co = PyAST_Compile(mod, filename, flags, arena);
1369 if (co == NULL)
1370 return NULL;
1371 v = PyEval_EvalCode(co, globals, locals);
1372 Py_DECREF(co);
1373 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001374}
1375
Guido van Rossum82598051997-03-05 00:20:32 +00001376static PyObject *
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001377run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001378 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00001379{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001380 PyCodeObject *co;
1381 PyObject *v;
1382 long magic;
1383 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00001384
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001385 magic = PyMarshal_ReadLongFromFile(fp);
1386 if (magic != PyImport_GetMagicNumber()) {
1387 PyErr_SetString(PyExc_RuntimeError,
1388 "Bad magic number in .pyc file");
1389 return NULL;
1390 }
1391 (void) PyMarshal_ReadLongFromFile(fp);
1392 v = PyMarshal_ReadLastObjectFromFile(fp);
1393 fclose(fp);
1394 if (v == NULL || !PyCode_Check(v)) {
1395 Py_XDECREF(v);
1396 PyErr_SetString(PyExc_RuntimeError,
1397 "Bad code object in .pyc file");
1398 return NULL;
1399 }
1400 co = (PyCodeObject *)v;
1401 v = PyEval_EvalCode(co, globals, locals);
1402 if (v && flags)
1403 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1404 Py_DECREF(co);
1405 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001406}
1407
Guido van Rossum82598051997-03-05 00:20:32 +00001408PyObject *
Tim Petersd08e3822003-04-17 15:24:21 +00001409Py_CompileStringFlags(const char *str, const char *filename, int start,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001411{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 PyCodeObject *co;
1413 mod_ty mod;
1414 PyArena *arena = PyArena_New();
1415 if (arena == NULL)
1416 return NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001418 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1419 if (mod == NULL) {
1420 PyArena_Free(arena);
1421 return NULL;
1422 }
1423 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1424 PyObject *result = PyAST_mod2obj(mod);
1425 PyArena_Free(arena);
1426 return result;
1427 }
1428 co = PyAST_Compile(mod, filename, flags, arena);
1429 PyArena_Free(arena);
1430 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00001431}
1432
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001433struct symtable *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001434Py_SymtableString(const char *str, const char *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001435{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001436 struct symtable *st;
1437 mod_ty mod;
1438 PyCompilerFlags flags;
1439 PyArena *arena = PyArena_New();
1440 if (arena == NULL)
1441 return NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +00001442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001443 flags.cf_flags = 0;
Christian Heimes7f23d862008-03-26 22:51:58 +00001444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001445 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1446 if (mod == NULL) {
1447 PyArena_Free(arena);
1448 return NULL;
1449 }
1450 st = PySymtable_Build(mod, filename, 0);
1451 PyArena_Free(arena);
1452 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00001453}
1454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455/* Preferred access to parser is through AST. */
1456mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001457PyParser_ASTFromString(const char *s, const char *filename, int start,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001458 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001460 mod_ty mod;
1461 PyCompilerFlags localflags;
1462 perrdetail err;
1463 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001464
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001465 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1466 &_PyParser_Grammar, start, &err,
1467 &iflags);
1468 if (flags == NULL) {
1469 localflags.cf_flags = 0;
1470 flags = &localflags;
1471 }
1472 if (n) {
1473 flags->cf_flags |= iflags & PyCF_MASK;
1474 mod = PyAST_FromNode(n, flags, filename, arena);
1475 PyNode_Free(n);
1476 return mod;
1477 }
1478 else {
1479 err_input(&err);
1480 return NULL;
1481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482}
1483
1484mod_ty
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001485PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001486 char *ps2, PyCompilerFlags *flags, int *errcode,
1487 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001489 mod_ty mod;
1490 PyCompilerFlags localflags;
1491 perrdetail err;
1492 int iflags = PARSER_FLAGS(flags);
Christian Heimes3c608332008-03-26 22:01:37 +00001493
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001494 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1495 start, ps1, ps2, &err, &iflags);
1496 if (flags == NULL) {
1497 localflags.cf_flags = 0;
1498 flags = &localflags;
1499 }
1500 if (n) {
1501 flags->cf_flags |= iflags & PyCF_MASK;
1502 mod = PyAST_FromNode(n, flags, filename, arena);
1503 PyNode_Free(n);
1504 return mod;
1505 }
1506 else {
1507 err_input(&err);
1508 if (errcode)
1509 *errcode = err.error;
1510 return NULL;
1511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512}
1513
Guido van Rossuma110aa61994-08-29 12:50:44 +00001514/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001515
Guido van Rossuma110aa61994-08-29 12:50:44 +00001516node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001517PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001518{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001519 perrdetail err;
1520 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1521 start, NULL, NULL, &err, flags);
1522 if (n == NULL)
1523 err_input(&err);
Tim Peters5e9d6cf2006-05-28 10:41:29 +00001524
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001525 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001526}
1527
Guido van Rossuma110aa61994-08-29 12:50:44 +00001528/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001529
Guido van Rossuma110aa61994-08-29 12:50:44 +00001530node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001531PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00001532{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533 perrdetail err;
1534 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1535 start, &err, flags);
1536 if (n == NULL)
1537 err_input(&err);
1538 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00001539}
1540
1541node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001542PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001543 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001544{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001545 perrdetail err;
1546 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1547 &_PyParser_Grammar, start, &err, flags);
1548 if (n == NULL)
1549 err_input(&err);
1550 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00001551}
1552
1553node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00001554PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00001555{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001556 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00001557}
1558
Guido van Rossum66ebd912003-04-17 16:02:26 +00001559/* May want to move a more generalized form of this to parsetok.c or
1560 even parser modules. */
1561
1562void
1563PyParser_SetError(perrdetail *err)
1564{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001565 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00001566}
1567
Guido van Rossuma110aa61994-08-29 12:50:44 +00001568/* Set the error appropriate to the given input error code (see errcode.h) */
1569
1570static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001571err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00001572{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001573 PyObject *v, *w, *errtype;
1574 PyObject* u = NULL;
1575 char *msg = NULL;
1576 errtype = PyExc_SyntaxError;
1577 switch (err->error) {
1578 case E_ERROR:
1579 return;
1580 case E_SYNTAX:
1581 errtype = PyExc_IndentationError;
1582 if (err->expected == INDENT)
1583 msg = "expected an indented block";
1584 else if (err->token == INDENT)
1585 msg = "unexpected indent";
1586 else if (err->token == DEDENT)
1587 msg = "unexpected unindent";
1588 else {
1589 errtype = PyExc_SyntaxError;
1590 msg = "invalid syntax";
1591 }
1592 break;
1593 case E_TOKEN:
1594 msg = "invalid token";
1595 break;
1596 case E_EOFS:
1597 msg = "EOF while scanning triple-quoted string literal";
1598 break;
1599 case E_EOLS:
1600 msg = "EOL while scanning string literal";
1601 break;
1602 case E_INTR:
1603 if (!PyErr_Occurred())
1604 PyErr_SetNone(PyExc_KeyboardInterrupt);
1605 goto cleanup;
1606 case E_NOMEM:
1607 PyErr_NoMemory();
1608 goto cleanup;
1609 case E_EOF:
1610 msg = "unexpected EOF while parsing";
1611 break;
1612 case E_TABSPACE:
1613 errtype = PyExc_TabError;
1614 msg = "inconsistent use of tabs and spaces in indentation";
1615 break;
1616 case E_OVERFLOW:
1617 msg = "expression too long";
1618 break;
1619 case E_DEDENT:
1620 errtype = PyExc_IndentationError;
1621 msg = "unindent does not match any outer indentation level";
1622 break;
1623 case E_TOODEEP:
1624 errtype = PyExc_IndentationError;
1625 msg = "too many levels of indentation";
1626 break;
1627 case E_DECODE: {
1628 PyObject *type, *value, *tb;
1629 PyErr_Fetch(&type, &value, &tb);
1630 if (value != NULL) {
1631 u = PyObject_Str(value);
1632 if (u != NULL) {
1633 msg = PyString_AsString(u);
1634 }
1635 }
1636 if (msg == NULL)
1637 msg = "unknown decode error";
1638 Py_XDECREF(type);
1639 Py_XDECREF(value);
1640 Py_XDECREF(tb);
1641 break;
1642 }
1643 case E_LINECONT:
1644 msg = "unexpected character after line continuation character";
1645 break;
1646 default:
1647 fprintf(stderr, "error=%d\n", err->error);
1648 msg = "unknown parsing error";
1649 break;
1650 }
1651 v = Py_BuildValue("(ziiz)", err->filename,
1652 err->lineno, err->offset, err->text);
1653 w = NULL;
1654 if (v != NULL)
1655 w = Py_BuildValue("(sO)", msg, v);
1656 Py_XDECREF(u);
1657 Py_XDECREF(v);
1658 PyErr_SetObject(errtype, w);
1659 Py_XDECREF(w);
Georg Brandl1ad108d2008-07-19 10:08:55 +00001660cleanup:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001661 if (err->text != NULL) {
1662 PyObject_FREE(err->text);
1663 err->text = NULL;
1664 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001665}
1666
1667/* Print fatal error message and abort */
1668
1669void
Tim Peters7c321a82002-07-09 02:57:01 +00001670Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001671{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001672 fprintf(stderr, "Fatal Python error: %s\n", msg);
1673 fflush(stderr); /* it helps in Windows debug build */
Jesse Noller42f9b4e2009-03-31 22:20:35 +00001674
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001675#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001676 {
1677 size_t len = strlen(msg);
1678 WCHAR* buffer;
1679 size_t i;
Martin v. Löwis5344c992009-01-02 20:32:55 +00001680
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001681 /* Convert the message to wchar_t. This uses a simple one-to-one
1682 conversion, assuming that the this error message actually uses ASCII
1683 only. If this ceases to be true, we will have to convert. */
1684 buffer = alloca( (len+1) * (sizeof *buffer));
1685 for( i=0; i<=len; ++i)
1686 buffer[i] = msg[i];
1687 OutputDebugStringW(L"Fatal Python error: ");
1688 OutputDebugStringW(buffer);
1689 OutputDebugStringW(L"\n");
1690 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00001691#ifdef _DEBUG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00001693#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001694#endif /* MS_WINDOWS */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001695 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001696}
1697
1698/* Clean up and exit */
1699
Guido van Rossuma110aa61994-08-29 12:50:44 +00001700#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00001701#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00001702#endif
1703
Antoine Pitrouefb60c02009-10-20 21:29:37 +00001704/* Wait until threading._shutdown completes, provided
1705 the threading module was imported in the first place.
1706 The shutdown routine will wait until all non-daemon
1707 "threading" threads have completed. */
1708static void
1709wait_for_thread_shutdown(void)
1710{
1711#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001712 PyObject *result;
1713 PyThreadState *tstate = PyThreadState_GET();
1714 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1715 "threading");
1716 if (threading == NULL) {
1717 /* threading not imported */
1718 PyErr_Clear();
1719 return;
1720 }
1721 result = PyObject_CallMethod(threading, "_shutdown", "");
1722 if (result == NULL)
1723 PyErr_WriteUnraisable(threading);
1724 else
1725 Py_DECREF(result);
1726 Py_DECREF(threading);
Antoine Pitrouefb60c02009-10-20 21:29:37 +00001727#endif
1728}
1729
Guido van Rossum2dcfc961998-10-01 16:01:57 +00001730#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001731static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00001732static int nexitfuncs = 0;
1733
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001734int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00001735{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001736 if (nexitfuncs >= NEXITFUNCS)
1737 return -1;
1738 exitfuncs[nexitfuncs++] = func;
1739 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00001740}
1741
Guido van Rossumcc283f51997-08-05 02:22:03 +00001742static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001743call_sys_exitfunc(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001744{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001745 PyObject *exitfunc = PySys_GetObject("exitfunc");
Guido van Rossum59bff391992-09-03 20:28:00 +00001746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001747 if (exitfunc) {
1748 PyObject *res;
1749 Py_INCREF(exitfunc);
1750 PySys_SetObject("exitfunc", (PyObject *)NULL);
1751 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1752 if (res == NULL) {
1753 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1754 PySys_WriteStderr("Error in sys.exitfunc:\n");
1755 }
1756 PyErr_Print();
1757 }
1758 Py_DECREF(exitfunc);
1759 }
Guido van Rossum59bff391992-09-03 20:28:00 +00001760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001761 if (Py_FlushLine())
1762 PyErr_Clear();
Guido van Rossumcc283f51997-08-05 02:22:03 +00001763}
Guido van Rossum1662dd51994-09-07 14:38:28 +00001764
Guido van Rossumcc283f51997-08-05 02:22:03 +00001765static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001766call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00001767{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001768 while (nexitfuncs > 0)
1769 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00001770
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001771 fflush(stdout);
1772 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001773}
1774
1775void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001776Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001777{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001778 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001779
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001780 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001781}
1782
Guido van Rossumf1dc5661993-07-05 10:31:29 +00001783static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001784initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001785{
Guido van Rossuma110aa61994-08-29 12:50:44 +00001786#ifdef SIGPIPE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001787 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001788#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00001789#ifdef SIGXFZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001790 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00001791#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001792#ifdef SIGXFSZ
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00001794#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001795 PyOS_InitInterrupts(); /* May imply initsignal() */
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00001796}
1797
Guido van Rossum7433b121997-02-14 19:45:36 +00001798
1799/*
1800 * The file descriptor fd is considered ``interactive'' if either
1801 * a) isatty(fd) is TRUE, or
1802 * b) the -i flag was given, and the filename associated with
1803 * the descriptor is NULL or "<stdin>" or "???".
1804 */
1805int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001806Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00001807{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001808 if (isatty((int)fileno(fp)))
1809 return 1;
1810 if (!Py_InteractiveFlag)
1811 return 0;
1812 return (filename == NULL) ||
1813 (strcmp(filename, "<stdin>") == 0) ||
1814 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00001815}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001816
1817
Tim Petersd08e3822003-04-17 15:24:21 +00001818#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001819#if defined(WIN32) && defined(_MSC_VER)
1820
1821/* Stack checking for Microsoft C */
1822
1823#include <malloc.h>
1824#include <excpt.h>
1825
Fred Drakee8de31c2000-08-31 05:38:39 +00001826/*
1827 * Return non-zero when we run out of memory on the stack; zero otherwise.
1828 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001829int
Fred Drake399739f2000-08-31 05:52:44 +00001830PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001831{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001832 __try {
1833 /* alloca throws a stack overflow exception if there's
1834 not enough space left on the stack */
1835 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1836 return 0;
1837 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1838 EXCEPTION_EXECUTE_HANDLER :
1839 EXCEPTION_CONTINUE_SEARCH) {
1840 int errcode = _resetstkoflw();
1841 if (errcode == 0)
1842 {
1843 Py_FatalError("Could not reset the stack!");
1844 }
1845 }
1846 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00001847}
1848
1849#endif /* WIN32 && _MSC_VER */
1850
1851/* Alternate implementations can be added here... */
1852
1853#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00001854
1855
1856/* Wrappers around sigaction() or signal(). */
1857
1858PyOS_sighandler_t
1859PyOS_getsig(int sig)
1860{
1861#ifdef HAVE_SIGACTION
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001862 struct sigaction context;
1863 if (sigaction(sig, NULL, &context) == -1)
1864 return SIG_ERR;
1865 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001866#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001868/* Special signal handling for the secure CRT in Visual Studio 2005 */
1869#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001870 switch (sig) {
1871 /* Only these signals are valid */
1872 case SIGINT:
1873 case SIGILL:
1874 case SIGFPE:
1875 case SIGSEGV:
1876 case SIGTERM:
1877 case SIGBREAK:
1878 case SIGABRT:
1879 break;
1880 /* Don't call signal() with other values or it will assert */
1881 default:
1882 return SIG_ERR;
1883 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00001884#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001885 handler = signal(sig, SIG_IGN);
1886 if (handler != SIG_ERR)
1887 signal(sig, handler);
1888 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001889#endif
1890}
1891
1892PyOS_sighandler_t
1893PyOS_setsig(int sig, PyOS_sighandler_t handler)
1894{
1895#ifdef HAVE_SIGACTION
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001896 /* Some code in Modules/signalmodule.c depends on sigaction() being
1897 * used here if HAVE_SIGACTION is defined. Fix that if this code
1898 * changes to invalidate that assumption.
1899 */
1900 struct sigaction context, ocontext;
1901 context.sa_handler = handler;
1902 sigemptyset(&context.sa_mask);
1903 context.sa_flags = 0;
1904 if (sigaction(sig, &context, &ocontext) == -1)
1905 return SIG_ERR;
1906 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001907#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001908 PyOS_sighandler_t oldhandler;
1909 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001910#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001911 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00001912#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001913 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00001914#endif
1915}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916
1917/* Deprecated C API functions still provided for binary compatiblity */
1918
1919#undef PyParser_SimpleParseFile
Thomas Heller1b046642006-04-18 18:51:06 +00001920PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1922{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001923 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924}
1925
Thomas Heller1b046642006-04-18 18:51:06 +00001926#undef PyParser_SimpleParseString
1927PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928PyParser_SimpleParseString(const char *str, int start)
1929{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001930 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931}
Anthony Baxterac6bd462006-04-13 02:06:09 +00001932
Thomas Heller1b046642006-04-18 18:51:06 +00001933#undef PyRun_AnyFile
1934PyAPI_FUNC(int)
1935PyRun_AnyFile(FILE *fp, const char *name)
1936{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001937 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001938}
1939
1940#undef PyRun_AnyFileEx
1941PyAPI_FUNC(int)
1942PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1943{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001944 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001945}
1946
1947#undef PyRun_AnyFileFlags
1948PyAPI_FUNC(int)
1949PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1950{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001951 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001952}
1953
1954#undef PyRun_File
1955PyAPI_FUNC(PyObject *)
1956PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1957{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001958 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001959}
1960
1961#undef PyRun_FileEx
1962PyAPI_FUNC(PyObject *)
1963PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1964{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001965 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001966}
1967
1968#undef PyRun_FileFlags
1969PyAPI_FUNC(PyObject *)
1970PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001971 PyCompilerFlags *flags)
Thomas Heller1b046642006-04-18 18:51:06 +00001972{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001973 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Heller1b046642006-04-18 18:51:06 +00001974}
1975
1976#undef PyRun_SimpleFile
1977PyAPI_FUNC(int)
1978PyRun_SimpleFile(FILE *f, const char *p)
1979{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001980 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001981}
1982
1983#undef PyRun_SimpleFileEx
1984PyAPI_FUNC(int)
1985PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1986{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001987 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001988}
1989
1990
1991#undef PyRun_String
1992PyAPI_FUNC(PyObject *)
1993PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1994{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001995 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00001996}
1997
1998#undef PyRun_SimpleString
1999PyAPI_FUNC(int)
2000PyRun_SimpleString(const char *s)
2001{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002002 return PyRun_SimpleStringFlags(s, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00002003}
2004
2005#undef Py_CompileString
2006PyAPI_FUNC(PyObject *)
2007Py_CompileString(const char *str, const char *p, int s)
2008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002009 return Py_CompileStringFlags(str, p, s, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00002010}
2011
2012#undef PyRun_InteractiveOne
2013PyAPI_FUNC(int)
2014PyRun_InteractiveOne(FILE *f, const char *p)
2015{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002016 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00002017}
2018
2019#undef PyRun_InteractiveLoop
2020PyAPI_FUNC(int)
2021PyRun_InteractiveLoop(FILE *f, const char *p)
2022{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002023 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Heller1b046642006-04-18 18:51:06 +00002024}
2025
Anthony Baxterac6bd462006-04-13 02:06:09 +00002026#ifdef __cplusplus
2027}
2028#endif
2029