blob: b5707c44048852353a7cf9827632bc67ae55a9dd [file] [log] [blame]
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001
2/* Python interpreter top-level routines, including init/exit */
3
Guido van Rossum82598051997-03-05 00:20:32 +00004#include "Python.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +00005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
Guido van Rossumd8faa362007-04-27 19:54:29 +00007#undef Yield /* undefine macro conflicting with winbase.h */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00008#include "grammar.h"
9#include "node.h"
Fred Drake85f36392000-07-11 17:53:00 +000010#include "token.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000011#include "parsetok.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +000012#include "errcode.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000014#include "symtable.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "ast.h"
Guido van Rossumfdef2711994-09-14 13:31:04 +000016#include "marshal.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000017#include "osdefs.h"
Victor Stinner518e6102014-03-18 02:06:38 +010018#include <locale.h>
Guido van Rossum1984f1e1992-08-04 12:41:02 +000019
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020#ifdef HAVE_SIGNAL_H
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000021#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022#endif
Guido van Rossuma9e7dc11992-10-18 18:53:57 +000023
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000024#ifdef MS_WINDOWS
Martin v. Löwis5c88d812009-01-02 20:47:48 +000025#include "malloc.h" /* for alloca */
Benjamin Peterson80a50ac2009-01-02 21:24:04 +000026#endif
Martin v. Löwis5c88d812009-01-02 20:47:48 +000027
Martin v. Löwis73d538b2003-03-05 15:13:47 +000028#ifdef HAVE_LANGINFO_H
Martin v. Löwis73d538b2003-03-05 15:13:47 +000029#include <langinfo.h>
30#endif
31
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000032#ifdef MS_WINDOWS
Guido van Rossuma44823b1995-03-14 15:01:17 +000033#undef BYTE
34#include "windows.h"
35#endif
36
Victor Stinnerbd303c12013-11-07 23:07:29 +010037_Py_IDENTIFIER(builtins);
Victor Stinner09054372013-11-06 22:41:44 +010038_Py_IDENTIFIER(excepthook);
Victor Stinner3f36a572013-11-12 21:39:02 +010039_Py_IDENTIFIER(flush);
Victor Stinnerbd303c12013-11-07 23:07:29 +010040_Py_IDENTIFIER(last_traceback);
Victor Stinner09054372013-11-06 22:41:44 +010041_Py_IDENTIFIER(last_type);
42_Py_IDENTIFIER(last_value);
Victor Stinner3f36a572013-11-12 21:39:02 +010043_Py_IDENTIFIER(name);
Victor Stinnerbd303c12013-11-07 23:07:29 +010044_Py_IDENTIFIER(ps1);
45_Py_IDENTIFIER(ps2);
46_Py_IDENTIFIER(stdin);
47_Py_IDENTIFIER(stdout);
48_Py_IDENTIFIER(stderr);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +010049_Py_static_string(PyId_string, "<string>");
Victor Stinner09054372013-11-06 22:41:44 +010050
Ezio Melotti1f8898a2013-03-26 01:59:56 +020051#ifdef Py_REF_DEBUG
Antoine Pitrou208ac5c2013-04-24 20:17:53 +020052static
53void _print_total_refs(void) {
Victor Stinner4ee41c52013-11-06 18:28:21 +010054 PyObject *xoptions, *value;
55 _Py_IDENTIFIER(showrefcount);
56
Ezio Melotti1f8898a2013-03-26 01:59:56 +020057 xoptions = PySys_GetXOptions();
58 if (xoptions == NULL)
59 return;
Victor Stinner4ee41c52013-11-06 18:28:21 +010060 value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
Ezio Melotti1f8898a2013-03-26 01:59:56 +020061 if (value == Py_True)
62 fprintf(stderr,
63 "[%" PY_FORMAT_SIZE_T "d refs, "
64 "%" PY_FORMAT_SIZE_T "d blocks]\n",
65 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
66}
67#endif
68
Neal Norwitz4281cef2006-03-04 19:58:13 +000069#ifndef Py_REF_DEBUG
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070#define PRINT_TOTAL_REFS()
Neal Norwitz4281cef2006-03-04 19:58:13 +000071#else /* Py_REF_DEBUG */
Ezio Melotti1f8898a2013-03-26 01:59:56 +020072#define PRINT_TOTAL_REFS() _print_total_refs()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073#endif
74
75#ifdef __cplusplus
76extern "C" {
Neal Norwitz4281cef2006-03-04 19:58:13 +000077#endif
78
Martin v. Löwis790465f2008-04-05 20:41:37 +000079extern wchar_t *Py_GetPath(void);
Guido van Rossum1984f1e1992-08-04 12:41:02 +000080
Guido van Rossum82598051997-03-05 00:20:32 +000081extern grammar _PyParser_Grammar; /* From graminit.c */
Guido van Rossum1984f1e1992-08-04 12:41:02 +000082
Guido van Rossumb73cc041993-11-01 16:28:59 +000083/* Forward */
Nick Coghlan85e729e2012-07-15 18:09:52 +100084static void initmain(PyInterpreterState *interp);
Victor Stinner793b5312011-04-27 00:24:21 +020085static int initfsencoding(PyInterpreterState *interp);
Tim Petersdbd9ba62000-07-09 03:09:57 +000086static void initsite(void);
Guido van Rossumce3a72a2007-10-19 23:16:50 +000087static int initstdio(void);
Amaury Forgeot d'Arc7fedbe52008-04-10 21:03:09 +000088static void flush_io(void);
Victor Stinner95701bd2013-11-06 18:41:07 +010089static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 PyCompilerFlags *, PyArena *);
Martin v. Löwis95292d62002-12-11 14:04:59 +000091static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000093static void err_input(perrdetail *);
Victor Stinner7f2fee32011-04-05 00:39:01 +020094static void err_free(perrdetail *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000095static void initsigs(void);
Collin Winter670e6922007-03-21 02:57:17 +000096static void call_py_exitfuncs(void);
Antoine Pitrou011bd622009-10-20 21:52:47 +000097static void wait_for_thread_shutdown(void);
Tim Petersdbd9ba62000-07-09 03:09:57 +000098static void call_ll_exitfuncs(void);
Victor Stinner3a50e702011-10-18 21:21:00 +020099extern int _PyUnicode_Init(void);
Victor Stinner26f91992013-07-17 01:22:45 +0200100extern int _PyStructSequence_Init(void);
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101extern void _PyUnicode_Fini(void);
Guido van Rossumddefaf32007-01-14 03:31:43 +0000102extern int _PyLong_Init(void);
103extern void PyLong_Fini(void);
Victor Stinner024e37a2011-03-31 01:31:06 +0200104extern int _PyFaulthandler_Init(void);
105extern void _PyFaulthandler_Fini(void);
Christian Heimes985ecdc2013-11-20 11:46:18 +0100106extern void _PyHash_Fini(void);
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100107extern int _PyTraceMalloc_Init(void);
Victor Stinnerbe0708f2013-12-01 10:03:26 +0100108extern int _PyTraceMalloc_Fini(void);
Guido van Rossumc94044c2000-03-10 23:03:54 +0000109
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000110#ifdef WITH_THREAD
111extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
112extern void _PyGILState_Fini(void);
113#endif /* WITH_THREAD */
114
Guido van Rossum82598051997-03-05 00:20:32 +0000115int Py_DebugFlag; /* Needed by parser.c */
116int Py_VerboseFlag; /* Needed by import.c */
Georg Brandl8aa7e992010-12-28 18:30:18 +0000117int Py_QuietFlag; /* Needed by sysmodule.c */
Guido van Rossum7433b121997-02-14 19:45:36 +0000118int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
Georg Brandl0b2489e2011-05-15 08:49:12 +0200119int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000120int Py_NoSiteFlag; /* Suppress 'import site' */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000121int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
Christian Heimes790c8232008-01-07 21:14:23 +0000122int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
Barry Warsaw3ce09642000-05-02 19:18:59 +0000123int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
Guido van Rossuma61691e1998-02-06 22:27:24 +0000124int Py_FrozenFlag; /* Needed by getpath.c */
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000125int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
Christian Heimes8dc226f2008-05-06 23:45:46 +0000126int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
Antoine Pitrou05608432009-01-09 18:53:14 +0000127int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100128int Py_HashRandomizationFlag = 0; /* for -R and PYTHONHASHSEED */
Christian Heimesad73a9c2013-08-10 16:36:18 +0200129int Py_IsolatedFlag = 0; /* for -I, isolate from user's env */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000130
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200131PyThreadState *_Py_Finalizing = NULL;
132
Christian Heimes49e61802013-10-22 10:22:29 +0200133/* Hack to force loading of object files */
134int (*_PyOS_mystrnicmp_hack)(const char *, const char *, Py_ssize_t) = \
135 PyOS_mystrnicmp; /* Python/pystrcmp.o */
136
Christian Heimes33fe8092008-04-13 13:53:33 +0000137/* PyModule_GetWarningsModule is no longer necessary as of 2.6
138since _warnings is builtin. This API should not be used. */
139PyObject *
140PyModule_GetWarningsModule(void)
Mark Hammondedd07732003-07-15 23:03:55 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return PyImport_ImportModule("warnings");
Mark Hammondedd07732003-07-15 23:03:55 +0000143}
Mark Hammonda43fd0c2003-02-19 00:33:33 +0000144
Guido van Rossum25ce5661997-08-02 03:10:38 +0000145static int initialized = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000146
Thomas Wouters7e474022000-07-16 12:04:32 +0000147/* API to access the initialized flag -- useful for esoteric use */
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000148
149int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000150Py_IsInitialized(void)
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000151{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return initialized;
Guido van Rossume3c0d5e1997-08-22 04:20:13 +0000153}
154
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000155/* Helper to allow an embedding application to override the normal
156 * mechanism that attempts to figure out an appropriate IO encoding
157 */
158
159static char *_Py_StandardStreamEncoding = NULL;
160static char *_Py_StandardStreamErrors = NULL;
161
162int
163Py_SetStandardStreamEncoding(const char *encoding, const char *errors)
164{
165 if (Py_IsInitialized()) {
166 /* This is too late to have any effect */
167 return -1;
168 }
Nick Coghlan1805a622013-10-18 23:11:47 +1000169 /* Can't call PyErr_NoMemory() on errors, as Python hasn't been
170 * initialised yet.
171 *
172 * However, the raw memory allocators are initialised appropriately
173 * as C static variables, so _PyMem_RawStrdup is OK even though
174 * Py_Initialize hasn't been called yet.
175 */
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000176 if (encoding) {
177 _Py_StandardStreamEncoding = _PyMem_RawStrdup(encoding);
178 if (!_Py_StandardStreamEncoding) {
Nick Coghlan1805a622013-10-18 23:11:47 +1000179 return -2;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000180 }
181 }
182 if (errors) {
183 _Py_StandardStreamErrors = _PyMem_RawStrdup(errors);
184 if (!_Py_StandardStreamErrors) {
185 if (_Py_StandardStreamEncoding) {
186 PyMem_RawFree(_Py_StandardStreamEncoding);
187 }
Nick Coghlan1805a622013-10-18 23:11:47 +1000188 return -3;
Nick Coghlan7d270ee2013-10-17 22:35:35 +1000189 }
190 }
191 return 0;
192}
193
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194/* Global initializations. Can be undone by Py_Finalize(). Don't
195 call this twice without an intervening Py_Finalize() call. When
196 initializations fail, a fatal error is issued and the function does
197 not return. On return, the first thread and interpreter state have
198 been created.
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000199
Guido van Rossum25ce5661997-08-02 03:10:38 +0000200 Locking: you must hold the interpreter lock while calling this.
201 (If the lock has not yet been initialized, that's equivalent to
202 having the lock, but you cannot use multiple threads.)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +0000203
Guido van Rossum25ce5661997-08-02 03:10:38 +0000204*/
Guido van Rossuma027efa1997-05-05 20:56:21 +0000205
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000206static int
207add_flag(int flag, const char *envs)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 int env = atoi(envs);
210 if (flag < env)
211 flag = env;
212 if (flag < 1)
213 flag = 1;
214 return flag;
Guido van Rossum9abaf4d2001-10-12 22:17:56 +0000215}
216
Christian Heimes5833a2f2008-10-30 21:40:04 +0000217static char*
Victor Stinner94908bb2010-08-18 21:23:25 +0000218get_codec_name(const char *encoding)
Christian Heimes5833a2f2008-10-30 21:40:04 +0000219{
Victor Stinner94908bb2010-08-18 21:23:25 +0000220 char *name_utf8, *name_str;
Victor Stinner386fe712010-05-19 00:34:15 +0000221 PyObject *codec, *name = NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000222
Victor Stinner94908bb2010-08-18 21:23:25 +0000223 codec = _PyCodec_Lookup(encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if (!codec)
225 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000226
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200227 name = _PyObject_GetAttrId(codec, &PyId_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 Py_CLEAR(codec);
229 if (!name)
230 goto error;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000231
Victor Stinner94908bb2010-08-18 21:23:25 +0000232 name_utf8 = _PyUnicode_AsString(name);
Victor Stinner4ca28092011-03-20 23:09:03 +0100233 if (name_utf8 == NULL)
Victor Stinner386fe712010-05-19 00:34:15 +0000234 goto error;
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200235 name_str = _PyMem_RawStrdup(name_utf8);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 Py_DECREF(name);
Victor Stinner94908bb2010-08-18 21:23:25 +0000237 if (name_str == NULL) {
238 PyErr_NoMemory();
239 return NULL;
240 }
241 return name_str;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000242
243error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 Py_XDECREF(codec);
Victor Stinner386fe712010-05-19 00:34:15 +0000245 Py_XDECREF(name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000247}
Victor Stinner94908bb2010-08-18 21:23:25 +0000248
Victor Stinner94908bb2010-08-18 21:23:25 +0000249static char*
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200250get_locale_encoding(void)
Victor Stinner94908bb2010-08-18 21:23:25 +0000251{
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200252#ifdef MS_WINDOWS
253 char codepage[100];
254 PyOS_snprintf(codepage, sizeof(codepage), "cp%d", GetACP());
255 return get_codec_name(codepage);
256#elif defined(HAVE_LANGINFO_H) && defined(CODESET)
Victor Stinner94908bb2010-08-18 21:23:25 +0000257 char* codeset = nl_langinfo(CODESET);
258 if (!codeset || codeset[0] == '\0') {
259 PyErr_SetString(PyExc_ValueError, "CODESET is not set or empty");
260 return NULL;
261 }
262 return get_codec_name(codeset);
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200263#else
264 PyErr_SetNone(PyExc_NotImplementedError);
265 return NULL;
Christian Heimes5833a2f2008-10-30 21:40:04 +0000266#endif
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200267}
Christian Heimes5833a2f2008-10-30 21:40:04 +0000268
Brett Cannonfd074152012-04-14 14:10:13 -0400269static void
270import_init(PyInterpreterState *interp, PyObject *sysmod)
271{
272 PyObject *importlib;
273 PyObject *impmod;
274 PyObject *sys_modules;
275 PyObject *value;
276
277 /* Import _importlib through its frozen version, _frozen_importlib. */
Brett Cannonfd074152012-04-14 14:10:13 -0400278 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
279 Py_FatalError("Py_Initialize: can't import _frozen_importlib");
280 }
281 else if (Py_VerboseFlag) {
282 PySys_FormatStderr("import _frozen_importlib # frozen\n");
283 }
284 importlib = PyImport_AddModule("_frozen_importlib");
285 if (importlib == NULL) {
286 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from "
287 "sys.modules");
288 }
289 interp->importlib = importlib;
290 Py_INCREF(interp->importlib);
291
292 /* Install _importlib as __import__ */
293 impmod = PyInit_imp();
294 if (impmod == NULL) {
295 Py_FatalError("Py_Initialize: can't import imp");
296 }
297 else if (Py_VerboseFlag) {
298 PySys_FormatStderr("import imp # builtin\n");
299 }
300 sys_modules = PyImport_GetModuleDict();
301 if (Py_VerboseFlag) {
302 PySys_FormatStderr("import sys # builtin\n");
303 }
Brett Cannon6f44d662012-04-15 16:08:47 -0400304 if (PyDict_SetItemString(sys_modules, "_imp", impmod) < 0) {
305 Py_FatalError("Py_Initialize: can't save _imp to sys.modules");
Brett Cannonfd074152012-04-14 14:10:13 -0400306 }
307
Brett Cannone0d88a12012-04-25 20:54:04 -0400308 value = PyObject_CallMethod(importlib, "_install", "OO", sysmod, impmod);
Brett Cannonfd074152012-04-14 14:10:13 -0400309 if (value == NULL) {
310 PyErr_Print();
311 Py_FatalError("Py_Initialize: importlib install failed");
312 }
313 Py_DECREF(value);
Brett Cannonfc9ca272012-04-15 01:35:05 -0400314 Py_DECREF(impmod);
Brett Cannonfd074152012-04-14 14:10:13 -0400315
316 _PyImportZip_Init();
317}
318
319
Guido van Rossuma027efa1997-05-05 20:56:21 +0000320void
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200321_Py_InitializeEx_Private(int install_sigs, int install_importlib)
Guido van Rossuma027efa1997-05-05 20:56:21 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 PyInterpreterState *interp;
324 PyThreadState *tstate;
325 PyObject *bimod, *sysmod, *pstderr;
326 char *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 extern void _Py_ReadyTypes(void);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (initialized)
330 return;
331 initialized = 1;
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200332 _Py_Finalizing = NULL;
Tim Petersd08e3822003-04-17 15:24:21 +0000333
Andrew M. Kuchlingb2ceb3a2010-02-22 23:26:10 +0000334#if defined(HAVE_LANGINFO_H) && defined(HAVE_SETLOCALE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* Set up the LC_CTYPE locale, so we can obtain
336 the locale's charset without having to switch
337 locales. */
338 setlocale(LC_CTYPE, "");
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +0000339#endif
340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
342 Py_DebugFlag = add_flag(Py_DebugFlag, p);
343 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
344 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
345 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
346 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
347 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
348 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100349 /* The variable is only tested for existence here; _PyRandom_Init will
350 check its value further. */
351 if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
352 Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
353
354 _PyRandom_Init();
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 interp = PyInterpreterState_New();
357 if (interp == NULL)
358 Py_FatalError("Py_Initialize: can't make first interpreter");
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 tstate = PyThreadState_New(interp);
361 if (tstate == NULL)
362 Py_FatalError("Py_Initialize: can't make first thread");
363 (void) PyThreadState_Swap(tstate);
Guido van Rossuma027efa1997-05-05 20:56:21 +0000364
Victor Stinner6961bd62010-08-17 22:26:51 +0000365#ifdef WITH_THREAD
Antoine Pitroub0b384b2010-09-20 20:13:48 +0000366 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
367 destroying the GIL might fail when it is being referenced from
368 another running thread (see issue #9901).
369 Instead we destroy the previously created GIL here, which ensures
370 that we can call Py_Initialize / Py_Finalize multiple times. */
371 _PyEval_FiniThreads();
372
373 /* Auto-thread-state API */
Victor Stinner6961bd62010-08-17 22:26:51 +0000374 _PyGILState_Init(interp, tstate);
375#endif /* WITH_THREAD */
376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 _Py_ReadyTypes();
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!_PyFrame_Init())
380 Py_FatalError("Py_Initialize: can't init frames");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (!_PyLong_Init())
383 Py_FatalError("Py_Initialize: can't init longs");
Neal Norwitzc91ed402002-12-30 22:29:22 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (!PyByteArray_Init())
386 Py_FatalError("Py_Initialize: can't init bytearray");
Neal Norwitz6968b052007-02-27 19:02:19 +0000387
Victor Stinner1c8f0592013-07-22 22:24:54 +0200388 if (!_PyFloat_Init())
389 Py_FatalError("Py_Initialize: can't init float");
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 interp->modules = PyDict_New();
392 if (interp->modules == NULL)
393 Py_FatalError("Py_Initialize: can't make modules dictionary");
Guido van Rossuma027efa1997-05-05 20:56:21 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 /* Init Unicode implementation; relies on the codec registry */
Victor Stinner3a50e702011-10-18 21:21:00 +0200396 if (_PyUnicode_Init() < 0)
397 Py_FatalError("Py_Initialize: can't initialize unicode");
Victor Stinner26f91992013-07-17 01:22:45 +0200398 if (_PyStructSequence_Init() < 0)
399 Py_FatalError("Py_Initialize: can't initialize structseq");
Guido van Rossumc94044c2000-03-10 23:03:54 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 bimod = _PyBuiltin_Init();
402 if (bimod == NULL)
403 Py_FatalError("Py_Initialize: can't initialize builtins modules");
Victor Stinner49d3f252010-10-17 01:24:53 +0000404 _PyImport_FixupBuiltin(bimod, "builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 interp->builtins = PyModule_GetDict(bimod);
406 if (interp->builtins == NULL)
407 Py_FatalError("Py_Initialize: can't initialize builtins dict");
408 Py_INCREF(interp->builtins);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400411 _PyExc_Init(bimod);
Christian Heimes9a68f8c2007-11-14 00:16:07 +0000412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 sysmod = _PySys_Init();
414 if (sysmod == NULL)
415 Py_FatalError("Py_Initialize: can't initialize sys");
416 interp->sysdict = PyModule_GetDict(sysmod);
417 if (interp->sysdict == NULL)
418 Py_FatalError("Py_Initialize: can't initialize sys dict");
419 Py_INCREF(interp->sysdict);
Victor Stinner49d3f252010-10-17 01:24:53 +0000420 _PyImport_FixupBuiltin(sysmod, "sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PySys_SetPath(Py_GetPath());
422 PyDict_SetItemString(interp->sysdict, "modules",
423 interp->modules);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 /* Set up a preliminary stderr printer until we have enough
426 infrastructure for the io module in place. */
427 pstderr = PyFile_NewStdPrinter(fileno(stderr));
428 if (pstderr == NULL)
429 Py_FatalError("Py_Initialize: can't set preliminary stderr");
Victor Stinnerbd303c12013-11-07 23:07:29 +0100430 _PySys_SetObjectId(&PyId_stderr, pstderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000432 Py_DECREF(pstderr);
Guido van Rossum826d8972007-10-30 18:34:07 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 _PyImport_Init();
Guido van Rossum7c85ab81999-07-08 17:26:56 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 _PyImportHooks_Init();
Just van Rossum52e14d62002-12-30 22:08:05 +0000437
Victor Stinner7d79b8b2010-05-19 20:40:50 +0000438 /* Initialize _warnings. */
439 _PyWarnings_Init();
440
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200441 if (!install_importlib)
442 return;
443
Brett Cannonfd074152012-04-14 14:10:13 -0400444 import_init(interp, sysmod);
445
Victor Stinnerd5698cb2012-07-31 02:55:49 +0200446 /* initialize the faulthandler module */
447 if (_PyFaulthandler_Init())
448 Py_FatalError("Py_Initialize: can't initialize faulthandler");
449
Victor Stinner00111242014-08-29 16:31:59 +0200450 if (_PyTime_Init() < 0)
451 Py_FatalError("Py_Initialize: can't initialize time");
Alexander Belopolsky6fc4ade2010-08-05 17:34:27 +0000452
Victor Stinner793b5312011-04-27 00:24:21 +0200453 if (initfsencoding(interp) < 0)
454 Py_FatalError("Py_Initialize: unable to load the file system codec");
Martin v. Löwis011e8422009-05-05 04:43:17 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (install_sigs)
457 initsigs(); /* Signal handling stuff, including initintr() */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000458
Victor Stinnered3b0bc2013-11-23 12:27:24 +0100459 if (_PyTraceMalloc_Init() < 0)
460 Py_FatalError("Py_Initialize: can't initialize tracemalloc");
461
Nick Coghlan85e729e2012-07-15 18:09:52 +1000462 initmain(interp); /* Module __main__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (initstdio() < 0)
464 Py_FatalError(
465 "Py_Initialize: can't initialize sys standard streams");
466
Antoine Pitroucf9f9802010-11-10 13:55:25 +0000467 /* Initialize warnings. */
468 if (PySys_HasWarnOptions()) {
469 PyObject *warnings_module = PyImport_ImportModule("warnings");
470 if (warnings_module == NULL) {
471 fprintf(stderr, "'import warnings' failed; traceback:\n");
472 PyErr_Print();
473 }
474 Py_XDECREF(warnings_module);
475 }
476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (!Py_NoSiteFlag)
478 initsite(); /* Module site */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000479}
480
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000481void
Antoine Pitroue67f48c2012-06-19 22:29:35 +0200482Py_InitializeEx(int install_sigs)
483{
484 _Py_InitializeEx_Private(install_sigs, 1);
485}
486
487void
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000488Py_Initialize(void)
489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 Py_InitializeEx(1);
Martin v. Löwis336e85f2004-08-19 11:31:58 +0000491}
492
493
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000494#ifdef COUNT_ALLOCS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495extern void dump_counts(FILE*);
Guido van Rossum2edcf0d1998-12-15 16:12:00 +0000496#endif
497
Guido van Rossume8432ac2007-07-09 15:04:50 +0000498/* Flush stdout and stderr */
499
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100500static int
501file_is_closed(PyObject *fobj)
502{
503 int r;
504 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
505 if (tmp == NULL) {
506 PyErr_Clear();
507 return 0;
508 }
509 r = PyObject_IsTrue(tmp);
510 Py_DECREF(tmp);
511 if (r < 0)
512 PyErr_Clear();
513 return r > 0;
514}
515
Neal Norwitz2bad9702007-08-27 06:19:22 +0000516static void
Guido van Rossum1bd21222007-07-10 20:14:13 +0000517flush_std_files(void)
Guido van Rossume8432ac2007-07-09 15:04:50 +0000518{
Victor Stinnerbd303c12013-11-07 23:07:29 +0100519 PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
520 PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyObject *tmp;
Guido van Rossume8432ac2007-07-09 15:04:50 +0000522
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100523 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200524 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (tmp == NULL)
Antoine Pitroubddc9fe2010-08-08 20:46:42 +0000526 PyErr_WriteUnraisable(fout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 else
528 Py_DECREF(tmp);
529 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000530
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100531 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200532 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (tmp == NULL)
534 PyErr_Clear();
535 else
536 Py_DECREF(tmp);
537 }
Guido van Rossume8432ac2007-07-09 15:04:50 +0000538}
539
Guido van Rossum25ce5661997-08-02 03:10:38 +0000540/* Undo the effect of Py_Initialize().
541
542 Beware: if multiple interpreter and/or thread states exist, these
543 are not wiped out; only the current thread and interpreter state
544 are deleted. But since everything else is deleted, those other
545 interpreter and thread states should no longer be used.
546
547 (XXX We should do better, e.g. wipe out all interpreters and
548 threads.)
549
550 Locking: as above.
551
552*/
553
554void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000555Py_Finalize(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyInterpreterState *interp;
558 PyThreadState *tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 if (!initialized)
561 return;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 wait_for_thread_shutdown();
Antoine Pitrou011bd622009-10-20 21:52:47 +0000564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* The interpreter is still entirely intact at this point, and the
566 * exit funcs may be relying on that. In particular, if some thread
567 * or exit func is still waiting to do an import, the import machinery
568 * expects Py_IsInitialized() to return true. So don't say the
569 * interpreter is uninitialized until after the exit funcs have run.
570 * Note that Threading.py uses an exit func to do a join on all the
571 * threads created thru it, so this also protects pending imports in
572 * the threads created via Threading.
573 */
574 call_py_exitfuncs();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 /* Get current thread state and interpreter pointer */
577 tstate = PyThreadState_GET();
578 interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000579
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200580 /* Remaining threads (e.g. daemon threads) will automatically exit
581 after taking the GIL (in PyEval_RestoreThread()). */
582 _Py_Finalizing = tstate;
583 initialized = 0;
584
Victor Stinner15054c12014-02-13 12:48:54 +0100585 /* Flush stdout+stderr */
586 flush_std_files();
587
588 /* Disable signal handling */
589 PyOS_FiniInterrupts();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Collect garbage. This may call finalizers; it's nice to call these
592 * before all modules are destroyed.
593 * XXX If a __del__ or weakref callback is triggered here, and tries to
594 * XXX import a module, bad things can happen, because Python no
595 * XXX longer believes it's initialized.
596 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
597 * XXX is easy to provoke that way. I've also seen, e.g.,
598 * XXX Exception exceptions.ImportError: 'No module named sha'
599 * XXX in <function callback at 0x008F5718> ignored
600 * XXX but I'm unclear on exactly how that one happens. In any case,
601 * XXX I haven't seen a real-life report of either of these.
602 */
603 PyGC_Collect();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* With COUNT_ALLOCS, it helps to run GC multiple times:
606 each collection might release some types from the type
607 list, so they become garbage. */
608 while (PyGC_Collect() > 0)
609 /* nothing */;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 /* Destroy all modules */
612 PyImport_Cleanup();
Guido van Rossum3a44e1b1997-11-03 21:58:47 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* Flush stdout+stderr (again, in case more was printed) */
615 flush_std_files();
Guido van Rossume8432ac2007-07-09 15:04:50 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* Collect final garbage. This disposes of cycles created by
Florent Xiclunaaa6c1d22011-12-12 18:54:29 +0100618 * class definitions, for example.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 * XXX This is disabled because it caused too many problems. If
620 * XXX a __del__ or weakref callback triggers here, Python code has
621 * XXX a hard time running, because even the sys module has been
622 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
623 * XXX One symptom is a sequence of information-free messages
624 * XXX coming from threads (if a __del__ or callback is invoked,
625 * XXX other threads can execute too, and any exception they encounter
626 * XXX triggers a comedy of errors as subsystem after subsystem
627 * XXX fails to find what it *expects* to find in sys to help report
628 * XXX the exception and consequent unexpected failures). I've also
629 * XXX seen segfaults then, after adding print statements to the
630 * XXX Python code getting called.
631 */
Tim Peters1d7323e2003-12-01 21:35:27 +0000632#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 PyGC_Collect();
Tim Peters1d7323e2003-12-01 21:35:27 +0000634#endif
Guido van Rossume13ddc92003-04-17 17:29:22 +0000635
Victor Stinnerbe0708f2013-12-01 10:03:26 +0100636 /* Disable tracemalloc after all Python objects have been destroyed,
637 so it is possible to use tracemalloc in objects destructor. */
638 _PyTraceMalloc_Fini();
639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
641 _PyImport_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000642
Antoine Pitroufd417cc2013-05-05 08:12:42 +0200643 /* Cleanup typeobject.c's internal caches. */
644 _PyType_Fini();
645
Victor Stinner024e37a2011-03-31 01:31:06 +0200646 /* unload faulthandler module */
647 _PyFaulthandler_Fini();
648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 /* Debugging stuff */
Guido van Rossum1707aad1997-12-08 23:43:45 +0000650#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 dump_counts(stdout);
Guido van Rossum1707aad1997-12-08 23:43:45 +0000652#endif
Christian Heimes985ecdc2013-11-20 11:46:18 +0100653 /* dump hash stats */
654 _PyHash_Fini();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PRINT_TOTAL_REFS();
Guido van Rossum1707aad1997-12-08 23:43:45 +0000657
Tim Peters9cf25ce2003-04-17 15:21:01 +0000658#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 /* Display all objects still alive -- this can invoke arbitrary
660 * __repr__ overrides, so requires a mostly-intact interpreter.
661 * Alas, a lot of stuff may still be alive now that will be cleaned
662 * up later.
663 */
664 if (Py_GETENV("PYTHONDUMPREFS"))
665 _Py_PrintReferences(stderr);
Tim Peters9cf25ce2003-04-17 15:21:01 +0000666#endif /* Py_TRACE_REFS */
667
Antoine Pitroufd417cc2013-05-05 08:12:42 +0200668 /* Clear interpreter state and all thread states. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyInterpreterState_Clear(interp);
Guido van Rossumd922fa42003-04-15 14:10:09 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 /* Now we decref the exception classes. After this point nothing
672 can raise an exception. That's okay, because each Fini() method
673 below has been checked to make sure no exceptions are ever
674 raised.
675 */
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 _PyExc_Fini();
Anthony Baxter12b6f6c2005-03-29 13:36:16 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 /* Sundry finalizers */
680 PyMethod_Fini();
681 PyFrame_Fini();
682 PyCFunction_Fini();
683 PyTuple_Fini();
684 PyList_Fini();
685 PySet_Fini();
686 PyBytes_Fini();
687 PyByteArray_Fini();
688 PyLong_Fini();
689 PyFloat_Fini();
690 PyDict_Fini();
Antoine Pitrouf34a0cd2011-11-18 20:14:34 +0100691 PySlice_Fini();
Antoine Pitrou5f454a02013-05-06 21:15:57 +0200692 _PyGC_Fini();
Antoine Pitrou4879a962013-08-31 00:26:02 +0200693 _PyRandom_Fini();
Guido van Rossumcc283f51997-08-05 02:22:03 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Cleanup Unicode implementation */
696 _PyUnicode_Fini();
Marc-André Lemburg95de5c12002-04-08 08:19:36 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* reset file system default encoding */
Victor Stinnerb744ba12010-05-15 12:27:16 +0000699 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200700 PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Py_FileSystemDefaultEncoding = NULL;
702 }
Christian Heimesc8967002007-11-30 10:18:26 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* XXX Still allocated:
705 - various static ad-hoc pointers to interned strings
706 - int and float free list blocks
707 - whatever various modules and libraries allocate
708 */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
Guido van Rossumcc283f51997-08-05 02:22:03 +0000711
Victor Stinner51fa4582013-07-07 15:50:49 +0200712 /* Cleanup auto-thread-state */
713#ifdef WITH_THREAD
714 _PyGILState_Fini();
715#endif /* WITH_THREAD */
716
717 /* Delete current thread. After this, many C API calls become crashy. */
718 PyThreadState_Swap(NULL);
719 PyInterpreterState_Delete(interp);
720
Tim Peters269b2a62003-04-17 19:52:29 +0000721#ifdef Py_TRACE_REFS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Display addresses (& refcnts) of all objects still alive.
723 * An address can be used to find the repr of the object, printed
724 * above by _Py_PrintReferences.
725 */
726 if (Py_GETENV("PYTHONDUMPREFS"))
727 _Py_PrintReferenceAddresses(stderr);
Tim Peters269b2a62003-04-17 19:52:29 +0000728#endif /* Py_TRACE_REFS */
Tim Peters0e871182002-04-13 08:29:14 +0000729#ifdef PYMALLOC_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 if (Py_GETENV("PYTHONMALLOCSTATS"))
David Malcolm49526f42012-06-22 14:55:41 -0400731 _PyObject_DebugMallocStats(stderr);
Tim Peters0e871182002-04-13 08:29:14 +0000732#endif
733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 call_ll_exitfuncs();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000735}
736
737/* Create and initialize a new interpreter and thread, and return the
738 new thread. This requires that Py_Initialize() has been called
739 first.
740
741 Unsuccessful initialization yields a NULL pointer. Note that *no*
742 exception information is available even in this case -- the
743 exception information is held in the thread, and there is no
744 thread.
745
746 Locking: as above.
747
748*/
749
750PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000751Py_NewInterpreter(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 PyInterpreterState *interp;
754 PyThreadState *tstate, *save_tstate;
755 PyObject *bimod, *sysmod;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 if (!initialized)
758 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 interp = PyInterpreterState_New();
761 if (interp == NULL)
762 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 tstate = PyThreadState_New(interp);
765 if (tstate == NULL) {
766 PyInterpreterState_Delete(interp);
767 return NULL;
768 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 save_tstate = PyThreadState_Swap(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 /* XXX The following is lax in error checking */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 interp->modules = PyDict_New();
Guido van Rossum25ce5661997-08-02 03:10:38 +0000775
Victor Stinner49d3f252010-10-17 01:24:53 +0000776 bimod = _PyImport_FindBuiltin("builtins");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (bimod != NULL) {
778 interp->builtins = PyModule_GetDict(bimod);
779 if (interp->builtins == NULL)
780 goto handle_error;
781 Py_INCREF(interp->builtins);
782 }
Christian Heimes6a27efa2008-10-30 21:48:26 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* initialize builtin exceptions */
Brett Cannonfd074152012-04-14 14:10:13 -0400785 _PyExc_Init(bimod);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000786
Victor Stinner49d3f252010-10-17 01:24:53 +0000787 sysmod = _PyImport_FindBuiltin("sys");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (bimod != NULL && sysmod != NULL) {
789 PyObject *pstderr;
Brett Cannonfd074152012-04-14 14:10:13 -0400790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 interp->sysdict = PyModule_GetDict(sysmod);
792 if (interp->sysdict == NULL)
793 goto handle_error;
794 Py_INCREF(interp->sysdict);
795 PySys_SetPath(Py_GetPath());
796 PyDict_SetItemString(interp->sysdict, "modules",
797 interp->modules);
798 /* Set up a preliminary stderr printer until we have enough
799 infrastructure for the io module in place. */
800 pstderr = PyFile_NewStdPrinter(fileno(stderr));
801 if (pstderr == NULL)
802 Py_FatalError("Py_Initialize: can't set preliminary stderr");
Victor Stinnerbd303c12013-11-07 23:07:29 +0100803 _PySys_SetObjectId(&PyId_stderr, pstderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PySys_SetObject("__stderr__", pstderr);
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +0000805 Py_DECREF(pstderr);
Christian Heimes6a27efa2008-10-30 21:48:26 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 _PyImportHooks_Init();
Victor Stinner793b5312011-04-27 00:24:21 +0200808
Brett Cannonfd074152012-04-14 14:10:13 -0400809 import_init(interp, sysmod);
810
Victor Stinner793b5312011-04-27 00:24:21 +0200811 if (initfsencoding(interp) < 0)
812 goto handle_error;
813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 if (initstdio() < 0)
815 Py_FatalError(
816 "Py_Initialize: can't initialize sys standard streams");
Nick Coghlan85e729e2012-07-15 18:09:52 +1000817 initmain(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (!Py_NoSiteFlag)
819 initsite();
820 }
Guido van Rossum25ce5661997-08-02 03:10:38 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (!PyErr_Occurred())
823 return tstate;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000824
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825handle_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 /* Oops, it didn't work. Undo it all. */
Guido van Rossum25ce5661997-08-02 03:10:38 +0000827
Victor Stinnerc40a3502011-04-27 00:20:27 +0200828 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyThreadState_Clear(tstate);
830 PyThreadState_Swap(save_tstate);
831 PyThreadState_Delete(tstate);
832 PyInterpreterState_Delete(interp);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 return NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000835}
836
837/* Delete an interpreter and its last thread. This requires that the
838 given thread state is current, that the thread has no remaining
839 frames, and that it is its interpreter's only remaining thread.
840 It is a fatal error to violate these constraints.
841
842 (Py_Finalize() doesn't have these constraints -- it zaps
843 everything, regardless.)
844
845 Locking: as above.
846
847*/
848
849void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000850Py_EndInterpreter(PyThreadState *tstate)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyInterpreterState *interp = tstate->interp;
Guido van Rossum25ce5661997-08-02 03:10:38 +0000853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 if (tstate != PyThreadState_GET())
855 Py_FatalError("Py_EndInterpreter: thread is not current");
856 if (tstate->frame != NULL)
857 Py_FatalError("Py_EndInterpreter: thread still has a frame");
Antoine Pitrou7eaf3f72013-08-25 19:48:18 +0200858
859 wait_for_thread_shutdown();
860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (tstate != interp->tstate_head || tstate->next != NULL)
862 Py_FatalError("Py_EndInterpreter: not the last thread");
Guido van Rossum25ce5661997-08-02 03:10:38 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 PyImport_Cleanup();
865 PyInterpreterState_Clear(interp);
866 PyThreadState_Swap(NULL);
867 PyInterpreterState_Delete(interp);
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000868}
869
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200870#ifdef MS_WINDOWS
Martin v. Löwis790465f2008-04-05 20:41:37 +0000871static wchar_t *progname = L"python";
Antoine Pitrou01cca5e2012-07-05 20:56:30 +0200872#else
873static wchar_t *progname = L"python3";
874#endif
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000875
876void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000877Py_SetProgramName(wchar_t *pn)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (pn && *pn)
880 progname = pn;
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000881}
882
Martin v. Löwis790465f2008-04-05 20:41:37 +0000883wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000884Py_GetProgramName(void)
Guido van Rossumad6dfda1997-07-19 19:17:22 +0000885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 return progname;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000887}
888
Martin v. Löwis790465f2008-04-05 20:41:37 +0000889static wchar_t *default_home = NULL;
Victor Stinner55a12202013-08-28 01:47:46 +0200890static wchar_t env_home[MAXPATHLEN+1];
Guido van Rossuma61691e1998-02-06 22:27:24 +0000891
892void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000893Py_SetPythonHome(wchar_t *home)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 default_home = home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000896}
897
Martin v. Löwis790465f2008-04-05 20:41:37 +0000898wchar_t *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000899Py_GetPythonHome(void)
Guido van Rossuma61691e1998-02-06 22:27:24 +0000900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 wchar_t *home = default_home;
902 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
903 char* chome = Py_GETENV("PYTHONHOME");
904 if (chome) {
Victor Stinner2f5bbc62013-11-15 17:09:24 +0100905 size_t size = Py_ARRAY_LENGTH(env_home);
906 size_t r = mbstowcs(env_home, chome, size);
907 if (r != (size_t)-1 && r < size)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 home = env_home;
909 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
912 return home;
Guido van Rossuma61691e1998-02-06 22:27:24 +0000913}
914
Guido van Rossum6135a871995-01-09 17:53:26 +0000915/* Create __main__ module */
916
917static void
Nick Coghlan85e729e2012-07-15 18:09:52 +1000918initmain(PyInterpreterState *interp)
Guido van Rossum6135a871995-01-09 17:53:26 +0000919{
Brett Cannon13853a62013-05-04 17:37:09 -0400920 PyObject *m, *d, *loader;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 m = PyImport_AddModule("__main__");
922 if (m == NULL)
923 Py_FatalError("can't create __main__ module");
924 d = PyModule_GetDict(m);
925 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
926 PyObject *bimod = PyImport_ImportModule("builtins");
Nick Coghlan85e729e2012-07-15 18:09:52 +1000927 if (bimod == NULL) {
928 Py_FatalError("Failed to retrieve builtins module");
929 }
930 if (PyDict_SetItemString(d, "__builtins__", bimod) < 0) {
931 Py_FatalError("Failed to initialize __main__.__builtins__");
932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 Py_DECREF(bimod);
934 }
Nick Coghlan85e729e2012-07-15 18:09:52 +1000935 /* Main is a little special - imp.is_builtin("__main__") will return
936 * False, but BuiltinImporter is still the most appropriate initial
937 * setting for its __loader__ attribute. A more suitable value will
938 * be set if __main__ gets further initialized later in the startup
939 * process.
940 */
Brett Cannon13853a62013-05-04 17:37:09 -0400941 loader = PyDict_GetItemString(d, "__loader__");
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400942 if (loader == NULL || loader == Py_None) {
Nick Coghlan85e729e2012-07-15 18:09:52 +1000943 PyObject *loader = PyObject_GetAttrString(interp->importlib,
944 "BuiltinImporter");
945 if (loader == NULL) {
946 Py_FatalError("Failed to retrieve BuiltinImporter");
947 }
948 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
949 Py_FatalError("Failed to initialize __main__.__loader__");
950 }
951 Py_DECREF(loader);
952 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000953}
954
Victor Stinner793b5312011-04-27 00:24:21 +0200955static int
956initfsencoding(PyInterpreterState *interp)
Victor Stinnerb744ba12010-05-15 12:27:16 +0000957{
958 PyObject *codec;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000959
Victor Stinnerd64e8a72011-07-04 13:48:30 +0200960 if (Py_FileSystemDefaultEncoding == NULL)
961 {
962 Py_FileSystemDefaultEncoding = get_locale_encoding();
963 if (Py_FileSystemDefaultEncoding == NULL)
Victor Stinnere4743092010-10-19 00:05:51 +0000964 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
Victor Stinnerb744ba12010-05-15 12:27:16 +0000965
Victor Stinnere4743092010-10-19 00:05:51 +0000966 Py_HasFileSystemDefaultEncoding = 0;
Victor Stinner793b5312011-04-27 00:24:21 +0200967 interp->fscodec_initialized = 1;
968 return 0;
Victor Stinner7f84ab52010-06-11 00:36:33 +0000969 }
Victor Stinnerb744ba12010-05-15 12:27:16 +0000970
971 /* the encoding is mbcs, utf-8 or ascii */
972 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
973 if (!codec) {
974 /* Such error can only occurs in critical situations: no more
975 * memory, import a module of the standard library failed,
976 * etc. */
Victor Stinner793b5312011-04-27 00:24:21 +0200977 return -1;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000978 }
Victor Stinner793b5312011-04-27 00:24:21 +0200979 Py_DECREF(codec);
980 interp->fscodec_initialized = 1;
981 return 0;
Victor Stinnerb744ba12010-05-15 12:27:16 +0000982}
983
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000984/* Import the site module (not into __main__ though) */
985
986static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000987initsite(void)
Guido van Rossumdcc0c131997-08-29 22:32:42 +0000988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 PyObject *m;
990 m = PyImport_ImportModule("site");
991 if (m == NULL) {
Victor Stinner62ce62a2013-07-22 22:53:28 +0200992 fprintf(stderr, "Failed to import the site module\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyErr_Print();
994 Py_Finalize();
995 exit(1);
996 }
997 else {
998 Py_DECREF(m);
999 }
Guido van Rossumdcc0c131997-08-29 22:32:42 +00001000}
1001
Antoine Pitrou05608432009-01-09 18:53:14 +00001002static PyObject*
1003create_stdio(PyObject* io,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 int fd, int write_mode, char* name,
1005 char* encoding, char* errors)
Antoine Pitrou05608432009-01-09 18:53:14 +00001006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
1008 const char* mode;
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00001009 const char* newline;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyObject *line_buffering;
1011 int buffering, isatty;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001012 _Py_IDENTIFIER(open);
1013 _Py_IDENTIFIER(isatty);
1014 _Py_IDENTIFIER(TextIOWrapper);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001015 _Py_IDENTIFIER(mode);
Antoine Pitrou05608432009-01-09 18:53:14 +00001016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 /* stdin is always opened in buffered mode, first because it shouldn't
1018 make a difference in common use cases, second because TextIOWrapper
1019 depends on the presence of a read1() method which only exists on
1020 buffered streams.
1021 */
1022 if (Py_UnbufferedStdioFlag && write_mode)
1023 buffering = 0;
1024 else
1025 buffering = -1;
1026 if (write_mode)
1027 mode = "wb";
1028 else
1029 mode = "rb";
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001030 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
1031 fd, mode, buffering,
1032 Py_None, Py_None, Py_None, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (buf == NULL)
1034 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 if (buffering) {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001037 _Py_IDENTIFIER(raw);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001038 raw = _PyObject_GetAttrId(buf, &PyId_raw);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 if (raw == NULL)
1040 goto error;
1041 }
1042 else {
1043 raw = buf;
1044 Py_INCREF(raw);
1045 }
Antoine Pitrou05608432009-01-09 18:53:14 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 text = PyUnicode_FromString(name);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001048 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 goto error;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001050 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 if (res == NULL)
1052 goto error;
1053 isatty = PyObject_IsTrue(res);
1054 Py_DECREF(res);
1055 if (isatty == -1)
1056 goto error;
1057 if (isatty || Py_UnbufferedStdioFlag)
1058 line_buffering = Py_True;
1059 else
1060 line_buffering = Py_False;
Antoine Pitrou91696412009-01-09 22:12:30 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 Py_CLEAR(raw);
1063 Py_CLEAR(text);
Antoine Pitrou91696412009-01-09 22:12:30 +00001064
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00001065#ifdef MS_WINDOWS
Victor Stinner7b3f0fa2012-08-04 01:28:00 +02001066 /* sys.stdin: enable universal newline mode, translate "\r\n" and "\r"
1067 newlines to "\n".
1068 sys.stdout and sys.stderr: translate "\n" to "\r\n". */
1069 newline = NULL;
1070#else
1071 /* sys.stdin: split lines at "\n".
1072 sys.stdout and sys.stderr: don't translate newlines (use "\n"). */
1073 newline = "\n";
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00001074#endif
1075
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001076 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
1077 buf, encoding, errors,
1078 newline, line_buffering);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 Py_CLEAR(buf);
1080 if (stream == NULL)
1081 goto error;
Antoine Pitrou05608432009-01-09 18:53:14 +00001082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 if (write_mode)
1084 mode = "w";
1085 else
1086 mode = "r";
1087 text = PyUnicode_FromString(mode);
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001088 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 goto error;
1090 Py_CLEAR(text);
1091 return stream;
Antoine Pitrou05608432009-01-09 18:53:14 +00001092
1093error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 Py_XDECREF(buf);
1095 Py_XDECREF(stream);
1096 Py_XDECREF(text);
1097 Py_XDECREF(raw);
1098 return NULL;
Antoine Pitrou05608432009-01-09 18:53:14 +00001099}
1100
Antoine Pitrou11942a52011-11-28 19:08:36 +01001101static int
1102is_valid_fd(int fd)
1103{
1104 int dummy_fd;
1105 if (fd < 0 || !_PyVerify_fd(fd))
1106 return 0;
1107 dummy_fd = dup(fd);
1108 if (dummy_fd < 0)
1109 return 0;
1110 close(dummy_fd);
1111 return 1;
1112}
1113
Georg Brandl1a3284e2007-12-02 09:40:06 +00001114/* Initialize sys.stdin, stdout, stderr and builtins.open */
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001115static int
1116initstdio(void)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 PyObject *iomod = NULL, *wrapper;
1119 PyObject *bimod = NULL;
1120 PyObject *m;
1121 PyObject *std = NULL;
1122 int status = 0, fd;
1123 PyObject * encoding_attr;
Serhiy Storchakabf28d2d2013-09-13 11:46:24 +03001124 char *pythonioencoding = NULL, *encoding, *errors;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 /* Hack to avoid a nasty recursion issue when Python is invoked
1127 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
1128 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
1129 goto error;
1130 }
1131 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
1134 goto error;
1135 }
1136 Py_DECREF(m);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (!(bimod = PyImport_ImportModule("builtins"))) {
1139 goto error;
1140 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (!(iomod = PyImport_ImportModule("io"))) {
1143 goto error;
1144 }
1145 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
1146 goto error;
1147 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* Set builtins.open */
1150 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001151 Py_DECREF(wrapper);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 goto error;
1153 }
Antoine Pitrou5a96b522010-11-20 19:50:57 +00001154 Py_DECREF(wrapper);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001155
Nick Coghlan7d270ee2013-10-17 22:35:35 +10001156 encoding = _Py_StandardStreamEncoding;
1157 errors = _Py_StandardStreamErrors;
1158 if (!encoding || !errors) {
Victor Stinner71430292014-03-18 01:18:21 +01001159 if (!errors) {
1160 /* When the LC_CTYPE locale is the POSIX locale ("C locale"),
1161 stdin and stdout use the surrogateescape error handler by
1162 default, instead of the strict error handler. */
1163 char *loc = setlocale(LC_CTYPE, NULL);
1164 if (loc != NULL && strcmp(loc, "C") == 0)
1165 errors = "surrogateescape";
1166 }
1167
Nick Coghlan7d270ee2013-10-17 22:35:35 +10001168 pythonioencoding = Py_GETENV("PYTHONIOENCODING");
1169 if (pythonioencoding) {
1170 char *err;
1171 pythonioencoding = _PyMem_Strdup(pythonioencoding);
1172 if (pythonioencoding == NULL) {
1173 PyErr_NoMemory();
1174 goto error;
1175 }
1176 err = strchr(pythonioencoding, ':');
1177 if (err) {
1178 *err = '\0';
1179 err++;
Victor Stinner71430292014-03-18 01:18:21 +01001180 if (*err && !_Py_StandardStreamErrors) {
Nick Coghlan7d270ee2013-10-17 22:35:35 +10001181 errors = err;
1182 }
1183 }
1184 if (*pythonioencoding && !encoding) {
1185 encoding = pythonioencoding;
1186 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
Martin v. Löwis0f599892008-06-02 11:13:03 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 /* Set sys.stdin */
1191 fd = fileno(stdin);
1192 /* Under some conditions stdin, stdout and stderr may not be connected
1193 * and fileno() may point to an invalid file descriptor. For example
1194 * GUI apps don't have valid standard streams by default.
1195 */
Antoine Pitrou11942a52011-11-28 19:08:36 +01001196 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 std = Py_None;
1198 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 }
1200 else {
1201 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
1202 if (std == NULL)
1203 goto error;
1204 } /* if (fd < 0) */
1205 PySys_SetObject("__stdin__", std);
Victor Stinnerbd303c12013-11-07 23:07:29 +01001206 _PySys_SetObjectId(&PyId_stdin, std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* Set sys.stdout */
1210 fd = fileno(stdout);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001211 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 std = Py_None;
1213 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 }
1215 else {
1216 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
1217 if (std == NULL)
1218 goto error;
1219 } /* if (fd < 0) */
1220 PySys_SetObject("__stdout__", std);
Victor Stinnerbd303c12013-11-07 23:07:29 +01001221 _PySys_SetObjectId(&PyId_stdout, std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 Py_DECREF(std);
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001223
Guido van Rossum98297ee2007-11-06 21:34:58 +00001224#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 /* Set sys.stderr, replaces the preliminary stderr */
1226 fd = fileno(stderr);
Antoine Pitrou11942a52011-11-28 19:08:36 +01001227 if (!is_valid_fd(fd)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 std = Py_None;
1229 Py_INCREF(std);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
1231 else {
1232 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
1233 if (std == NULL)
1234 goto error;
1235 } /* if (fd < 0) */
Trent Nelson39e307e2008-03-19 06:45:48 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Same as hack above, pre-import stderr's codec to avoid recursion
1238 when import.c tries to write to stderr in verbose mode. */
1239 encoding_attr = PyObject_GetAttrString(std, "encoding");
1240 if (encoding_attr != NULL) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001241 const char * std_encoding;
1242 std_encoding = _PyUnicode_AsString(encoding_attr);
1243 if (std_encoding != NULL) {
1244 PyObject *codec_info = _PyCodec_Lookup(std_encoding);
Antoine Pitrou2fabfac2012-01-18 15:14:46 +01001245 Py_XDECREF(codec_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 }
Hirokazu Yamamotodaf83ac2010-10-30 15:08:15 +00001247 Py_DECREF(encoding_attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 }
1249 PyErr_Clear(); /* Not a fatal error if codec isn't available */
Trent Nelson39e307e2008-03-19 06:45:48 +00001250
Victor Stinnerba308832013-07-22 23:55:19 +02001251 if (PySys_SetObject("__stderr__", std) < 0) {
1252 Py_DECREF(std);
1253 goto error;
1254 }
Victor Stinnerbd303c12013-11-07 23:07:29 +01001255 if (_PySys_SetObjectId(&PyId_stderr, std) < 0) {
Victor Stinnerba308832013-07-22 23:55:19 +02001256 Py_DECREF(std);
1257 goto error;
1258 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 Py_DECREF(std);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001260#endif
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001263 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 status = -1;
1265 }
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001266
Nick Coghlan7d270ee2013-10-17 22:35:35 +10001267 /* We won't need them anymore. */
1268 if (_Py_StandardStreamEncoding) {
1269 PyMem_RawFree(_Py_StandardStreamEncoding);
1270 _Py_StandardStreamEncoding = NULL;
1271 }
1272 if (_Py_StandardStreamErrors) {
1273 PyMem_RawFree(_Py_StandardStreamErrors);
1274 _Py_StandardStreamErrors = NULL;
1275 }
Serhiy Storchakabf28d2d2013-09-13 11:46:24 +03001276 PyMem_Free(pythonioencoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 Py_XDECREF(bimod);
1278 Py_XDECREF(iomod);
1279 return status;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001280}
1281
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001282/* Parse input from a file and execute it */
1283
1284int
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001285PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (filename == NULL)
1289 filename = "???";
1290 if (Py_FdIsInteractive(fp, filename)) {
1291 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
1292 if (closeit)
1293 fclose(fp);
1294 return err;
1295 }
1296 else
1297 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001298}
1299
1300int
Victor Stinner95701bd2013-11-06 18:41:07 +01001301PyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001302{
Victor Stinner95701bd2013-11-06 18:41:07 +01001303 PyObject *filename, *v;
1304 int ret, err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyCompilerFlags local_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001306
Victor Stinner95701bd2013-11-06 18:41:07 +01001307 filename = PyUnicode_DecodeFSDefault(filename_str);
1308 if (filename == NULL) {
1309 PyErr_Print();
1310 return -1;
1311 }
1312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (flags == NULL) {
1314 flags = &local_flags;
1315 local_flags.cf_flags = 0;
1316 }
Victor Stinner09054372013-11-06 22:41:44 +01001317 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +01001319 _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 Py_XDECREF(v);
1321 }
Victor Stinner09054372013-11-06 22:41:44 +01001322 v = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (v == NULL) {
Victor Stinner09054372013-11-06 22:41:44 +01001324 _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 Py_XDECREF(v);
1326 }
Victor Stinner95701bd2013-11-06 18:41:07 +01001327 err = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 for (;;) {
Victor Stinner95701bd2013-11-06 18:41:07 +01001329 ret = PyRun_InteractiveOneObject(fp, filename, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PRINT_TOTAL_REFS();
Victor Stinner95701bd2013-11-06 18:41:07 +01001331 if (ret == E_EOF) {
1332 err = 0;
1333 break;
1334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /*
1336 if (ret == E_NOMEM)
Victor Stinner95701bd2013-11-06 18:41:07 +01001337 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 */
1339 }
Victor Stinner95701bd2013-11-06 18:41:07 +01001340 Py_DECREF(filename);
1341 return err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001342}
1343
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001344/* compute parser flags based on compiler flags */
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001345static int PARSER_FLAGS(PyCompilerFlags *flags)
1346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 int parser_flags = 0;
1348 if (!flags)
1349 return 0;
1350 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1351 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1352 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1353 parser_flags |= PyPARSE_IGNORE_COOKIE;
1354 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1355 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1356 return parser_flags;
Benjamin Petersonf5b52242009-03-02 23:31:26 +00001357}
Neil Schemenauerc24ea082002-03-22 23:53:36 +00001358
Thomas Wouters89f507f2006-12-13 04:49:30 +00001359#if 0
1360/* Keep an example of flags with future keyword support. */
1361#define PARSER_FLAGS(flags) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
1363 PyPARSE_DONT_IMPLY_DEDENT : 0) \
1364 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
1365 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001366#endif
1367
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001368int
Victor Stinner95701bd2013-11-06 18:41:07 +01001369PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
Jeremy Hylton9f324e92001-03-01 22:59:14 +00001370{
Victor Stinner95701bd2013-11-06 18:41:07 +01001371 PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 mod_ty mod;
1373 PyArena *arena;
1374 char *ps1 = "", *ps2 = "", *enc = NULL;
1375 int errcode = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001376 _Py_IDENTIFIER(encoding);
Victor Stinner95701bd2013-11-06 18:41:07 +01001377 _Py_IDENTIFIER(__main__);
1378
1379 mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
1380 if (mod_name == NULL) {
1381 PyErr_Print();
1382 return -1;
1383 }
Tim Petersfe2127d2001-07-16 05:37:24 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (fp == stdin) {
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -04001386 /* Fetch encoding from sys.stdin if possible. */
Victor Stinnerbd303c12013-11-07 23:07:29 +01001387 v = _PySys_GetObjectId(&PyId_stdin);
Benjamin Petersonfe1b22a2013-04-29 10:23:08 -04001388 if (v && v != Py_None) {
1389 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1390 if (oenc)
1391 enc = _PyUnicode_AsString(oenc);
1392 if (!enc)
1393 PyErr_Clear();
1394 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 }
Victor Stinner09054372013-11-06 22:41:44 +01001396 v = _PySys_GetObjectId(&PyId_ps1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (v != NULL) {
1398 v = PyObject_Str(v);
1399 if (v == NULL)
1400 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001401 else if (PyUnicode_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 ps1 = _PyUnicode_AsString(v);
Victor Stinner386fe712010-05-19 00:34:15 +00001403 if (ps1 == NULL) {
1404 PyErr_Clear();
1405 ps1 = "";
1406 }
1407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 }
Victor Stinner09054372013-11-06 22:41:44 +01001409 w = _PySys_GetObjectId(&PyId_ps2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (w != NULL) {
1411 w = PyObject_Str(w);
1412 if (w == NULL)
1413 PyErr_Clear();
Victor Stinner386fe712010-05-19 00:34:15 +00001414 else if (PyUnicode_Check(w)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 ps2 = _PyUnicode_AsString(w);
Victor Stinner386fe712010-05-19 00:34:15 +00001416 if (ps2 == NULL) {
1417 PyErr_Clear();
1418 ps2 = "";
1419 }
1420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 }
1422 arena = PyArena_New();
1423 if (arena == NULL) {
1424 Py_XDECREF(v);
1425 Py_XDECREF(w);
1426 Py_XDECREF(oenc);
1427 return -1;
1428 }
Victor Stinner95701bd2013-11-06 18:41:07 +01001429 mod = PyParser_ASTFromFileObject(fp, filename, enc,
1430 Py_single_input, ps1, ps2,
1431 flags, &errcode, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 Py_XDECREF(v);
1433 Py_XDECREF(w);
1434 Py_XDECREF(oenc);
1435 if (mod == NULL) {
1436 PyArena_Free(arena);
1437 if (errcode == E_EOF) {
1438 PyErr_Clear();
1439 return E_EOF;
1440 }
1441 PyErr_Print();
1442 return -1;
1443 }
Victor Stinner95701bd2013-11-06 18:41:07 +01001444 m = PyImport_AddModuleObject(mod_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (m == NULL) {
1446 PyArena_Free(arena);
1447 return -1;
1448 }
1449 d = PyModule_GetDict(m);
1450 v = run_mod(mod, filename, d, d, flags, arena);
1451 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (v == NULL) {
1453 PyErr_Print();
Antoine Pitrou9845c7e2014-05-11 13:42:17 +02001454 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 return -1;
1456 }
1457 Py_DECREF(v);
Antoine Pitrou9845c7e2014-05-11 13:42:17 +02001458 flush_io();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001460}
1461
Victor Stinner95701bd2013-11-06 18:41:07 +01001462int
1463PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
1464{
1465 PyObject *filename;
1466 int res;
1467
1468 filename = PyUnicode_DecodeFSDefault(filename_str);
1469 if (filename == NULL) {
1470 PyErr_Print();
1471 return -1;
1472 }
1473 res = PyRun_InteractiveOneObject(fp, filename, flags);
1474 Py_DECREF(filename);
1475 return res;
1476}
1477
1478
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001479/* Check whether a file maybe a pyc file: Look at the extension,
1480 the file type, and, if we may close it, at the first few bytes. */
1481
1482static int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001483maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1486 return 1;
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Only look into the file if we are allowed to close it, since
1489 it then should also be seekable. */
1490 if (closeit) {
1491 /* Read only two bytes of the magic. If the file was opened in
1492 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1493 be read as they are on disk. */
1494 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1495 unsigned char buf[2];
1496 /* Mess: In case of -x, the stream is NOT at its start now,
1497 and ungetc() was used to push back the first newline,
1498 which makes the current stream position formally undefined,
1499 and a x-platform nightmare.
1500 Unfortunately, we have no direct way to know whether -x
1501 was specified. So we use a terrible hack: if the current
1502 stream position is not 0, we assume -x was specified, and
1503 give up. Bug 132850 on SourceForge spells out the
1504 hopelessness of trying anything else (fseek and ftell
1505 don't work predictably x-platform for text-mode files).
1506 */
1507 int ispyc = 0;
1508 if (ftell(fp) == 0) {
1509 if (fread(buf, 1, 2, fp) == 2 &&
1510 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1511 ispyc = 1;
1512 rewind(fp);
1513 }
1514 return ispyc;
1515 }
1516 return 0;
Tim Petersd08e3822003-04-17 15:24:21 +00001517}
Martin v. Löwisbe4c0f52001-01-04 20:30:56 +00001518
Antoine Pitrou32d483c2013-07-30 21:01:23 +02001519static int
1520set_main_loader(PyObject *d, const char *filename, const char *loader_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +10001521{
1522 PyInterpreterState *interp;
1523 PyThreadState *tstate;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001524 PyObject *filename_obj, *loader_type, *loader;
Nick Coghlanb7a58942012-07-15 23:21:08 +10001525 int result = 0;
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001526
1527 filename_obj = PyUnicode_DecodeFSDefault(filename);
1528 if (filename_obj == NULL)
1529 return -1;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001530 /* Get current thread state and interpreter pointer */
1531 tstate = PyThreadState_GET();
1532 interp = tstate->interp;
Nick Coghlan3f94cbf2012-07-15 19:10:39 +10001533 loader_type = PyObject_GetAttrString(interp->importlib, loader_name);
1534 if (loader_type == NULL) {
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001535 Py_DECREF(filename_obj);
Nick Coghlan3f94cbf2012-07-15 19:10:39 +10001536 return -1;
1537 }
Andrew Svetlov90c0eb22012-11-01 14:51:14 +02001538 loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
Nick Coghlanb7a58942012-07-15 23:21:08 +10001539 Py_DECREF(loader_type);
1540 if (loader == NULL) {
Nick Coghlan85e729e2012-07-15 18:09:52 +10001541 return -1;
1542 }
Nick Coghlanb7a58942012-07-15 23:21:08 +10001543 if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
1544 result = -1;
1545 }
Nick Coghlan85e729e2012-07-15 18:09:52 +10001546 Py_DECREF(loader);
Nick Coghlanb7a58942012-07-15 23:21:08 +10001547 return result;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001548}
1549
1550int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001551PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00001553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 PyObject *m, *d, *v;
1555 const char *ext;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001556 int set_file_name = 0, ret = -1;
Victor Stinner0fcab4a2011-01-04 12:59:15 +00001557 size_t len;
Guido van Rossumfdef2711994-09-14 13:31:04 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 m = PyImport_AddModule("__main__");
1560 if (m == NULL)
1561 return -1;
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001562 Py_INCREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 d = PyModule_GetDict(m);
1564 if (PyDict_GetItemString(d, "__file__") == NULL) {
1565 PyObject *f;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00001566 f = PyUnicode_DecodeFSDefault(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 if (f == NULL)
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001568 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1570 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001571 goto done;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 }
Barry Warsaw916048d2011-09-20 14:45:44 -04001573 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1574 Py_DECREF(f);
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001575 goto done;
Barry Warsaw916048d2011-09-20 14:45:44 -04001576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 set_file_name = 1;
1578 Py_DECREF(f);
1579 }
1580 len = strlen(filename);
1581 ext = filename + len - (len > 4 ? 4 : 0);
1582 if (maybe_pyc_file(fp, filename, ext, closeit)) {
Christian Heimes04ac4c12012-09-11 15:47:28 +02001583 FILE *pyc_fp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 /* Try to run a pyc file. First, re-open in binary */
1585 if (closeit)
1586 fclose(fp);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001587 if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 fprintf(stderr, "python: Can't reopen .pyc file\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 goto done;
1590 }
1591 /* Turn on optimization if a .pyo file is given */
1592 if (strcmp(ext, ".pyo") == 0)
1593 Py_OptimizeFlag = 1;
Nick Coghlan85e729e2012-07-15 18:09:52 +10001594
1595 if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
1596 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1597 ret = -1;
Christian Heimes04ac4c12012-09-11 15:47:28 +02001598 fclose(pyc_fp);
Nick Coghlan85e729e2012-07-15 18:09:52 +10001599 goto done;
1600 }
Christian Heimes04ac4c12012-09-11 15:47:28 +02001601 v = run_pyc_file(pyc_fp, filename, d, d, flags);
1602 fclose(pyc_fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 } else {
Nick Coghlan85e729e2012-07-15 18:09:52 +10001604 /* When running from stdin, leave __main__.__loader__ alone */
1605 if (strcmp(filename, "<stdin>") != 0 &&
1606 set_main_loader(d, filename, "SourceFileLoader") < 0) {
1607 fprintf(stderr, "python: failed to set __main__.__loader__\n");
1608 ret = -1;
1609 goto done;
1610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1612 closeit, flags);
1613 }
1614 flush_io();
1615 if (v == NULL) {
1616 PyErr_Print();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 goto done;
1618 }
1619 Py_DECREF(v);
1620 ret = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001621 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1623 PyErr_Clear();
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +01001624 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return ret;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001626}
1627
1628int
Martin v. Löwis95292d62002-12-11 14:04:59 +00001629PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
Guido van Rossum393661d2001-08-31 17:40:15 +00001630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PyObject *m, *d, *v;
1632 m = PyImport_AddModule("__main__");
1633 if (m == NULL)
1634 return -1;
1635 d = PyModule_GetDict(m);
1636 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1637 if (v == NULL) {
1638 PyErr_Print();
1639 return -1;
1640 }
1641 Py_DECREF(v);
1642 return 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001643}
1644
Barry Warsaw035574d1997-08-29 22:07:17 +00001645static int
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001646parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
1647 int *lineno, int *offset, PyObject **text)
Barry Warsaw035574d1997-08-29 22:07:17 +00001648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 long hold;
1650 PyObject *v;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001651 _Py_IDENTIFIER(msg);
1652 _Py_IDENTIFIER(filename);
1653 _Py_IDENTIFIER(lineno);
1654 _Py_IDENTIFIER(offset);
1655 _Py_IDENTIFIER(text);
Barry Warsaw035574d1997-08-29 22:07:17 +00001656
Benjamin Peterson80d50422012-04-03 00:30:38 -04001657 *message = NULL;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001658 *filename = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 /* new style errors. `err' is an instance */
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001661 *message = _PyObject_GetAttrId(err, &PyId_msg);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001662 if (!*message)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 goto finally;
Barry Warsaw035574d1997-08-29 22:07:17 +00001664
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001665 v = _PyObject_GetAttrId(err, &PyId_filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001666 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001668 if (v == Py_None) {
1669 Py_DECREF(v);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001670 *filename = _PyUnicode_FromId(&PyId_string);
1671 if (*filename == NULL)
1672 goto finally;
1673 Py_INCREF(*filename);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001674 }
1675 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001676 *filename = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001677 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001678
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001679 v = _PyObject_GetAttrId(err, &PyId_lineno);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001680 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 goto finally;
1682 hold = PyLong_AsLong(v);
1683 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (hold < 0 && PyErr_Occurred())
1685 goto finally;
1686 *lineno = (int)hold;
Barry Warsaw035574d1997-08-29 22:07:17 +00001687
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001688 v = _PyObject_GetAttrId(err, &PyId_offset);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001689 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 goto finally;
1691 if (v == Py_None) {
1692 *offset = -1;
1693 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 } else {
1695 hold = PyLong_AsLong(v);
1696 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 if (hold < 0 && PyErr_Occurred())
1698 goto finally;
1699 *offset = (int)hold;
1700 }
Barry Warsaw035574d1997-08-29 22:07:17 +00001701
Benjamin Peterson0a9a6362012-04-03 00:35:36 -04001702 v = _PyObject_GetAttrId(err, &PyId_text);
Benjamin Peterson80d50422012-04-03 00:30:38 -04001703 if (!v)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 goto finally;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001705 if (v == Py_None) {
1706 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 *text = NULL;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001708 }
1709 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001710 *text = v;
Benjamin Peterson80d50422012-04-03 00:30:38 -04001711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return 1;
Barry Warsaw035574d1997-08-29 22:07:17 +00001713
1714finally:
Benjamin Peterson80d50422012-04-03 00:30:38 -04001715 Py_XDECREF(*message);
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001716 Py_XDECREF(*filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return 0;
Barry Warsaw035574d1997-08-29 22:07:17 +00001718}
1719
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001720void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001721PyErr_Print(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 PyErr_PrintEx(1);
Guido van Rossuma61691e1998-02-06 22:27:24 +00001724}
1725
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001726static void
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001727print_error_text(PyObject *f, int offset, PyObject *text_obj)
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001728{
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001729 char *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 char *nl;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001731
1732 text = _PyUnicode_AsString(text_obj);
1733 if (text == NULL)
1734 return;
1735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 if (offset >= 0) {
Victor Stinner98ea54c2014-08-15 23:30:40 +02001737 if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
Benjamin Petersona95e9772010-10-29 03:28:14 +00001738 offset--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 for (;;) {
1740 nl = strchr(text, '\n');
1741 if (nl == NULL || nl-text >= offset)
1742 break;
1743 offset -= (int)(nl+1-text);
1744 text = nl+1;
1745 }
1746 while (*text == ' ' || *text == '\t') {
1747 text++;
1748 offset--;
1749 }
1750 }
1751 PyFile_WriteString(" ", f);
1752 PyFile_WriteString(text, f);
1753 if (*text == '\0' || text[strlen(text)-1] != '\n')
1754 PyFile_WriteString("\n", f);
1755 if (offset == -1)
1756 return;
1757 PyFile_WriteString(" ", f);
Benjamin Petersona95e9772010-10-29 03:28:14 +00001758 while (--offset > 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 PyFile_WriteString(" ", f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 PyFile_WriteString("^\n", f);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00001761}
1762
Guido van Rossum66e8e862001-03-23 17:54:43 +00001763static void
1764handle_system_exit(void)
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 PyObject *exception, *value, *tb;
1767 int exitcode = 0;
Tim Peterscf615b52003-04-19 18:47:02 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (Py_InspectFlag)
1770 /* Don't exit if -i flag was given. This flag is set to 0
1771 * when entering interactive mode for inspecting. */
1772 return;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 PyErr_Fetch(&exception, &value, &tb);
1775 fflush(stdout);
1776 if (value == NULL || value == Py_None)
1777 goto done;
1778 if (PyExceptionInstance_Check(value)) {
1779 /* The error code should be in the `code' attribute. */
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001780 _Py_IDENTIFIER(code);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001781 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (code) {
1783 Py_DECREF(value);
1784 value = code;
1785 if (value == Py_None)
1786 goto done;
1787 }
1788 /* If we failed to dig out the 'code' attribute,
1789 just let the else clause below print the error. */
1790 }
1791 if (PyLong_Check(value))
1792 exitcode = (int)PyLong_AsLong(value);
1793 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001794 PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
Nick Coghland979e432014-02-09 10:43:21 +10001795 /* We clear the exception here to avoid triggering the assertion
1796 * in PyObject_Str that ensures it won't silently lose exception
1797 * details.
1798 */
1799 PyErr_Clear();
Victor Stinner7126dbc2010-05-21 23:45:42 +00001800 if (sys_stderr != NULL && sys_stderr != Py_None) {
1801 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1802 } else {
1803 PyObject_Print(value, stderr, Py_PRINT_RAW);
1804 fflush(stderr);
1805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 PySys_WriteStderr("\n");
1807 exitcode = 1;
1808 }
Tim Peterscf615b52003-04-19 18:47:02 +00001809 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Restore and clear the exception info, in order to properly decref
1811 * the exception, value, and traceback. If we just exit instead,
1812 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1813 * some finalizers from running.
1814 */
1815 PyErr_Restore(exception, value, tb);
1816 PyErr_Clear();
1817 Py_Exit(exitcode);
1818 /* NOTREACHED */
Ka-Ping Yee26fabb02001-03-23 15:36:41 +00001819}
1820
1821void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001822PyErr_PrintEx(int set_sys_last_vars)
Guido van Rossuma61691e1998-02-06 22:27:24 +00001823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 PyObject *exception, *v, *tb, *hook;
Guido van Rossum66e8e862001-03-23 17:54:43 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1827 handle_system_exit();
1828 }
1829 PyErr_Fetch(&exception, &v, &tb);
1830 if (exception == NULL)
1831 return;
1832 PyErr_NormalizeException(&exception, &v, &tb);
1833 if (tb == NULL) {
1834 tb = Py_None;
1835 Py_INCREF(tb);
1836 }
1837 PyException_SetTraceback(v, tb);
1838 if (exception == NULL)
1839 return;
1840 /* Now we know v != NULL too */
1841 if (set_sys_last_vars) {
Victor Stinner09054372013-11-06 22:41:44 +01001842 _PySys_SetObjectId(&PyId_last_type, exception);
1843 _PySys_SetObjectId(&PyId_last_value, v);
1844 _PySys_SetObjectId(&PyId_last_traceback, tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 }
Victor Stinner09054372013-11-06 22:41:44 +01001846 hook = _PySys_GetObjectId(&PyId_excepthook);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (hook) {
1848 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1849 PyObject *result = PyEval_CallObject(hook, args);
1850 if (result == NULL) {
1851 PyObject *exception2, *v2, *tb2;
1852 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1853 handle_system_exit();
1854 }
1855 PyErr_Fetch(&exception2, &v2, &tb2);
1856 PyErr_NormalizeException(&exception2, &v2, &tb2);
1857 /* It should not be possible for exception2 or v2
1858 to be NULL. However PyErr_Display() can't
1859 tolerate NULLs, so just be safe. */
1860 if (exception2 == NULL) {
1861 exception2 = Py_None;
1862 Py_INCREF(exception2);
1863 }
1864 if (v2 == NULL) {
1865 v2 = Py_None;
1866 Py_INCREF(v2);
1867 }
1868 fflush(stdout);
1869 PySys_WriteStderr("Error in sys.excepthook:\n");
1870 PyErr_Display(exception2, v2, tb2);
1871 PySys_WriteStderr("\nOriginal exception was:\n");
1872 PyErr_Display(exception, v, tb);
1873 Py_DECREF(exception2);
1874 Py_DECREF(v2);
1875 Py_XDECREF(tb2);
1876 }
1877 Py_XDECREF(result);
1878 Py_XDECREF(args);
1879 } else {
1880 PySys_WriteStderr("sys.excepthook is missing\n");
1881 PyErr_Display(exception, v, tb);
1882 }
1883 Py_XDECREF(exception);
1884 Py_XDECREF(v);
1885 Py_XDECREF(tb);
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00001886}
1887
Benjamin Petersone6528212008-07-15 15:32:09 +00001888static void
1889print_exception(PyObject *f, PyObject *value)
1890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 int err = 0;
1892 PyObject *type, *tb;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001893 _Py_IDENTIFIER(print_file_and_line);
Benjamin Petersone6528212008-07-15 15:32:09 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 if (!PyExceptionInstance_Check(value)) {
Victor Stinner52ce3b02013-12-09 02:10:08 +01001896 err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1897 err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1898 err += PyFile_WriteString(" found\n", f);
1899 if (err)
1900 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 return;
1902 }
Benjamin Peterson26582602008-08-23 20:08:07 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 Py_INCREF(value);
1905 fflush(stdout);
1906 type = (PyObject *) Py_TYPE(value);
1907 tb = PyException_GetTraceback(value);
1908 if (tb && tb != Py_None)
1909 err = PyTraceBack_Print(tb, f);
1910 if (err == 0 &&
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02001911 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001913 PyObject *message, *filename, *text;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 int lineno, offset;
1915 if (!parse_syntax_error(value, &message, &filename,
1916 &lineno, &offset, &text))
1917 PyErr_Clear();
1918 else {
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001919 PyObject *line;
1920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 Py_DECREF(value);
1922 value = message;
Victor Stinnerefa7a0e2013-11-07 12:37:56 +01001923
1924 line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
1925 filename, lineno);
1926 Py_DECREF(filename);
1927 if (line != NULL) {
1928 PyFile_WriteObject(line, f, Py_PRINT_RAW);
1929 Py_DECREF(line);
1930 }
1931
1932 if (text != NULL) {
1933 print_error_text(f, offset, text);
1934 Py_DECREF(text);
1935 }
1936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 /* Can't be bothered to check all those
1938 PyFile_WriteString() calls */
1939 if (PyErr_Occurred())
1940 err = -1;
1941 }
1942 }
1943 if (err) {
1944 /* Don't do anything else */
1945 }
1946 else {
1947 PyObject* moduleName;
1948 char* className;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001949 _Py_IDENTIFIER(__module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 assert(PyExceptionClass_Check(type));
1951 className = PyExceptionClass_Name(type);
1952 if (className != NULL) {
1953 char *dot = strrchr(className, '.');
1954 if (dot != NULL)
1955 className = dot+1;
1956 }
Benjamin Petersone6528212008-07-15 15:32:09 +00001957
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001958 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1960 {
Victor Stinner13b21bd2011-05-26 14:25:13 +02001961 Py_XDECREF(moduleName);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 err = PyFile_WriteString("<unknown>", f);
1963 }
1964 else {
Victor Stinnerbd303c12013-11-07 23:07:29 +01001965 if (_PyUnicode_CompareWithId(moduleName, &PyId_builtins) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 {
Victor Stinner937114f2013-11-07 00:12:30 +01001967 err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 err += PyFile_WriteString(".", f);
1969 }
1970 Py_DECREF(moduleName);
1971 }
1972 if (err == 0) {
1973 if (className == NULL)
1974 err = PyFile_WriteString("<unknown>", f);
1975 else
1976 err = PyFile_WriteString(className, f);
1977 }
1978 }
1979 if (err == 0 && (value != Py_None)) {
1980 PyObject *s = PyObject_Str(value);
1981 /* only print colon if the str() of the
1982 object is not the empty string
1983 */
1984 if (s == NULL)
1985 err = -1;
1986 else if (!PyUnicode_Check(s) ||
Victor Stinnere251d6d2011-11-20 19:20:00 +01001987 PyUnicode_GetLength(s) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 err = PyFile_WriteString(": ", f);
1989 if (err == 0)
1990 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1991 Py_XDECREF(s);
1992 }
1993 /* try to write a newline in any case */
1994 err += PyFile_WriteString("\n", f);
1995 Py_XDECREF(tb);
1996 Py_DECREF(value);
1997 /* If an error happened here, don't show it.
1998 XXX This is wrong, but too many callers rely on this behavior. */
1999 if (err != 0)
2000 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00002001}
2002
2003static const char *cause_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 "\nThe above exception was the direct cause "
2005 "of the following exception:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00002006
2007static const char *context_message =
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 "\nDuring handling of the above exception, "
2009 "another exception occurred:\n\n";
Benjamin Petersone6528212008-07-15 15:32:09 +00002010
2011static void
2012print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
2013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 int err = 0, res;
2015 PyObject *cause, *context;
Benjamin Petersone6528212008-07-15 15:32:09 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (seen != NULL) {
2018 /* Exception chaining */
2019 if (PySet_Add(seen, value) == -1)
2020 PyErr_Clear();
2021 else if (PyExceptionInstance_Check(value)) {
2022 cause = PyException_GetCause(value);
2023 context = PyException_GetContext(value);
Benjamin Petersond5a1c442012-05-14 22:09:31 -07002024 if (cause) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 res = PySet_Contains(seen, cause);
2026 if (res == -1)
2027 PyErr_Clear();
2028 if (res == 0) {
2029 print_exception_recursive(
2030 f, cause, seen);
2031 err |= PyFile_WriteString(
2032 cause_message, f);
2033 }
2034 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07002035 else if (context &&
2036 !((PyBaseExceptionObject *)value)->suppress_context) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 res = PySet_Contains(seen, context);
2038 if (res == -1)
2039 PyErr_Clear();
2040 if (res == 0) {
2041 print_exception_recursive(
2042 f, context, seen);
2043 err |= PyFile_WriteString(
2044 context_message, f);
2045 }
2046 }
2047 Py_XDECREF(context);
2048 Py_XDECREF(cause);
2049 }
2050 }
2051 print_exception(f, value);
2052 if (err != 0)
2053 PyErr_Clear();
Benjamin Petersone6528212008-07-15 15:32:09 +00002054}
2055
Thomas Wouters477c8d52006-05-27 19:21:47 +00002056void
2057PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +00002058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 PyObject *seen;
Victor Stinnerbd303c12013-11-07 23:07:29 +01002060 PyObject *f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrou24201d42013-10-13 21:53:13 +02002061 if (PyExceptionInstance_Check(value)
2062 && tb != NULL && PyTraceBack_Check(tb)) {
2063 /* Put the traceback on the exception, otherwise it won't get
2064 displayed. See issue #18776. */
2065 PyObject *cur_tb = PyException_GetTraceback(value);
2066 if (cur_tb == NULL)
2067 PyException_SetTraceback(value, tb);
2068 else
2069 Py_DECREF(cur_tb);
2070 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 if (f == Py_None) {
2072 /* pass */
2073 }
2074 else if (f == NULL) {
2075 _PyObject_Dump(value);
2076 fprintf(stderr, "lost sys.stderr\n");
2077 }
2078 else {
2079 /* We choose to ignore seen being possibly NULL, and report
2080 at least the main exception (it could be a MemoryError).
2081 */
2082 seen = PySet_New(NULL);
2083 if (seen == NULL)
2084 PyErr_Clear();
2085 print_exception_recursive(f, value, seen);
2086 Py_XDECREF(seen);
2087 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002088}
2089
Guido van Rossum82598051997-03-05 00:20:32 +00002090PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002091PyRun_StringFlags(const char *str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 PyObject *ret = NULL;
2095 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01002096 PyArena *arena;
Victor Stinner95701bd2013-11-06 18:41:07 +01002097 PyObject *filename;
2098
2099 filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
2100 if (filename == NULL)
2101 return NULL;
2102
2103 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 if (arena == NULL)
2105 return NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002106
Victor Stinner95701bd2013-11-06 18:41:07 +01002107 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (mod != NULL)
Victor Stinner95701bd2013-11-06 18:41:07 +01002109 ret = run_mod(mod, filename, globals, locals, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 PyArena_Free(arena);
2111 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00002112}
2113
2114PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01002115PyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 PyObject *locals, int closeit, PyCompilerFlags *flags)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00002117{
Victor Stinner95701bd2013-11-06 18:41:07 +01002118 PyObject *ret = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 mod_ty mod;
Victor Stinner95701bd2013-11-06 18:41:07 +01002120 PyArena *arena = NULL;
2121 PyObject *filename;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002122
Victor Stinner95701bd2013-11-06 18:41:07 +01002123 filename = PyUnicode_DecodeFSDefault(filename_str);
2124 if (filename == NULL)
2125 goto exit;
2126
2127 arena = PyArena_New();
2128 if (arena == NULL)
2129 goto exit;
2130
2131 mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
2132 flags, NULL, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 if (closeit)
2134 fclose(fp);
2135 if (mod == NULL) {
Victor Stinner95701bd2013-11-06 18:41:07 +01002136 goto exit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002137 }
2138 ret = run_mod(mod, filename, globals, locals, flags, arena);
Victor Stinner95701bd2013-11-06 18:41:07 +01002139
2140exit:
2141 Py_XDECREF(filename);
2142 if (arena != NULL)
2143 PyArena_Free(arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 return ret;
Jeremy Hyltonbc320242001-03-22 02:47:58 +00002145}
2146
Guido van Rossum6c193fa2007-12-05 05:14:58 +00002147static void
2148flush_io(void)
2149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 PyObject *f, *r;
2151 PyObject *type, *value, *traceback;
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00002152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 /* Save the current exception */
2154 PyErr_Fetch(&type, &value, &traceback);
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00002155
Victor Stinnerbd303c12013-11-07 23:07:29 +01002156 f = _PySys_GetObjectId(&PyId_stderr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002158 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 if (r)
2160 Py_DECREF(r);
2161 else
2162 PyErr_Clear();
2163 }
Victor Stinnerbd303c12013-11-07 23:07:29 +01002164 f = _PySys_GetObjectId(&PyId_stdout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 if (f != NULL) {
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002166 r = _PyObject_CallMethodId(f, &PyId_flush, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 if (r)
2168 Py_DECREF(r);
2169 else
2170 PyErr_Clear();
2171 }
Amaury Forgeot d'Arc9ed77352008-04-04 23:25:27 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 PyErr_Restore(type, value, traceback);
Guido van Rossum6c193fa2007-12-05 05:14:58 +00002174}
2175
Guido van Rossum82598051997-03-05 00:20:32 +00002176static PyObject *
Victor Stinner95701bd2013-11-06 18:41:07 +01002177run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
2178 PyCompilerFlags *flags, PyArena *arena)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 PyCodeObject *co;
2181 PyObject *v;
Victor Stinner95701bd2013-11-06 18:41:07 +01002182 co = PyAST_CompileObject(mod, filename, flags, -1, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 if (co == NULL)
2184 return NULL;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002185 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 Py_DECREF(co);
2187 return v;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002188}
2189
Guido van Rossum82598051997-03-05 00:20:32 +00002190static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002191run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyObject *locals, PyCompilerFlags *flags)
Guido van Rossumfdef2711994-09-14 13:31:04 +00002193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 PyCodeObject *co;
2195 PyObject *v;
2196 long magic;
2197 long PyImport_GetMagicNumber(void);
Guido van Rossumfdef2711994-09-14 13:31:04 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 magic = PyMarshal_ReadLongFromFile(fp);
2200 if (magic != PyImport_GetMagicNumber()) {
2201 PyErr_SetString(PyExc_RuntimeError,
2202 "Bad magic number in .pyc file");
2203 return NULL;
2204 }
Antoine Pitrou5136ac02012-01-13 18:52:16 +01002205 /* Skip mtime and size */
2206 (void) PyMarshal_ReadLongFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 (void) PyMarshal_ReadLongFromFile(fp);
2208 v = PyMarshal_ReadLastObjectFromFile(fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 if (v == NULL || !PyCode_Check(v)) {
2210 Py_XDECREF(v);
2211 PyErr_SetString(PyExc_RuntimeError,
2212 "Bad code object in .pyc file");
2213 return NULL;
2214 }
2215 co = (PyCodeObject *)v;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002216 v = PyEval_EvalCode((PyObject*)co, globals, locals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 if (v && flags)
2218 flags->cf_flags |= (co->co_flags & PyCF_MASK);
2219 Py_DECREF(co);
2220 return v;
Guido van Rossumfdef2711994-09-14 13:31:04 +00002221}
2222
Guido van Rossum82598051997-03-05 00:20:32 +00002223PyObject *
Victor Stinner14e461d2013-08-26 22:28:21 +02002224Py_CompileStringObject(const char *str, PyObject *filename, int start,
2225 PyCompilerFlags *flags, int optimize)
Jeremy Hyltonbc320242001-03-22 02:47:58 +00002226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyCodeObject *co;
2228 mod_ty mod;
2229 PyArena *arena = PyArena_New();
2230 if (arena == NULL)
2231 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002232
Victor Stinner14e461d2013-08-26 22:28:21 +02002233 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 if (mod == NULL) {
2235 PyArena_Free(arena);
2236 return NULL;
2237 }
2238 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
2239 PyObject *result = PyAST_mod2obj(mod);
2240 PyArena_Free(arena);
2241 return result;
2242 }
Victor Stinner14e461d2013-08-26 22:28:21 +02002243 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 PyArena_Free(arena);
2245 return (PyObject *)co;
Guido van Rossum5b722181993-03-30 17:46:03 +00002246}
2247
Victor Stinner14e461d2013-08-26 22:28:21 +02002248PyObject *
2249Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
2250 PyCompilerFlags *flags, int optimize)
2251{
2252 PyObject *filename, *co;
2253 filename = PyUnicode_DecodeFSDefault(filename_str);
2254 if (filename == NULL)
2255 return NULL;
2256 co = Py_CompileStringObject(str, filename, start, flags, optimize);
2257 Py_DECREF(filename);
2258 return co;
2259}
2260
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00002261/* For use in Py_LIMITED_API */
2262#undef Py_CompileString
2263PyObject *
2264PyCompileString(const char *str, const char *filename, int start)
2265{
2266 return Py_CompileStringFlags(str, filename, start, NULL);
2267}
2268
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002269struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +02002270Py_SymtableStringObject(const char *str, PyObject *filename, int start)
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 struct symtable *st;
2273 mod_ty mod;
2274 PyCompilerFlags flags;
Victor Stinner14e461d2013-08-26 22:28:21 +02002275 PyArena *arena;
2276
2277 arena = PyArena_New();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 if (arena == NULL)
2279 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 flags.cf_flags = 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002282 mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 if (mod == NULL) {
2284 PyArena_Free(arena);
2285 return NULL;
2286 }
Victor Stinner14e461d2013-08-26 22:28:21 +02002287 st = PySymtable_BuildObject(mod, filename, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 PyArena_Free(arena);
2289 return st;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002290}
2291
Victor Stinner14e461d2013-08-26 22:28:21 +02002292struct symtable *
2293Py_SymtableString(const char *str, const char *filename_str, int start)
2294{
2295 PyObject *filename;
2296 struct symtable *st;
2297
2298 filename = PyUnicode_DecodeFSDefault(filename_str);
2299 if (filename == NULL)
2300 return NULL;
2301 st = Py_SymtableStringObject(str, filename, start);
2302 Py_DECREF(filename);
2303 return st;
2304}
2305
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306/* Preferred access to parser is through AST. */
2307mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02002308PyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
2309 PyCompilerFlags *flags, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 mod_ty mod;
2312 PyCompilerFlags localflags;
2313 perrdetail err;
2314 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002315
Victor Stinner14e461d2013-08-26 22:28:21 +02002316 node *n = PyParser_ParseStringObject(s, filename,
2317 &_PyParser_Grammar, start, &err,
2318 &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 if (flags == NULL) {
2320 localflags.cf_flags = 0;
2321 flags = &localflags;
2322 }
2323 if (n) {
2324 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02002325 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 }
2328 else {
2329 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002330 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002332 err_free(&err);
2333 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334}
2335
2336mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +02002337PyParser_ASTFromString(const char *s, const char *filename_str, int start,
2338 PyCompilerFlags *flags, PyArena *arena)
2339{
2340 PyObject *filename;
2341 mod_ty mod;
2342 filename = PyUnicode_DecodeFSDefault(filename_str);
2343 if (filename == NULL)
2344 return NULL;
2345 mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
2346 Py_DECREF(filename);
2347 return mod;
2348}
2349
2350mod_ty
2351PyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
2352 int start, char *ps1,
2353 char *ps2, PyCompilerFlags *flags, int *errcode,
2354 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 mod_ty mod;
2357 PyCompilerFlags localflags;
2358 perrdetail err;
2359 int iflags = PARSER_FLAGS(flags);
Christian Heimes4d6ec852008-03-26 22:34:47 +00002360
Victor Stinner14e461d2013-08-26 22:28:21 +02002361 node *n = PyParser_ParseFileObject(fp, filename, enc,
2362 &_PyParser_Grammar,
2363 start, ps1, ps2, &err, &iflags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 if (flags == NULL) {
2365 localflags.cf_flags = 0;
2366 flags = &localflags;
2367 }
2368 if (n) {
2369 flags->cf_flags |= iflags & PyCF_MASK;
Victor Stinner14e461d2013-08-26 22:28:21 +02002370 mod = PyAST_FromNodeObject(n, flags, filename, arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 PyNode_Free(n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 }
2373 else {
2374 err_input(&err);
2375 if (errcode)
2376 *errcode = err.error;
Victor Stinner7f2fee32011-04-05 00:39:01 +02002377 mod = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002379 err_free(&err);
2380 return mod;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381}
2382
Victor Stinner14e461d2013-08-26 22:28:21 +02002383mod_ty
2384PyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
2385 int start, char *ps1,
2386 char *ps2, PyCompilerFlags *flags, int *errcode,
2387 PyArena *arena)
2388{
2389 mod_ty mod;
2390 PyObject *filename;
2391 filename = PyUnicode_DecodeFSDefault(filename_str);
2392 if (filename == NULL)
2393 return NULL;
2394 mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
2395 flags, errcode, arena);
2396 Py_DECREF(filename);
2397 return mod;
2398}
2399
Guido van Rossuma110aa61994-08-29 12:50:44 +00002400/* Simplified interface to parsefile -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002401
Guido van Rossuma110aa61994-08-29 12:50:44 +00002402node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002403PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 perrdetail err;
2406 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
2407 &_PyParser_Grammar,
2408 start, NULL, NULL, &err, flags);
2409 if (n == NULL)
2410 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002411 err_free(&err);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 return n;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002414}
2415
Guido van Rossuma110aa61994-08-29 12:50:44 +00002416/* Simplified interface to parsestring -- return node or set exception */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002417
Guido van Rossuma110aa61994-08-29 12:50:44 +00002418node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002419PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +00002420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 perrdetail err;
2422 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
2423 start, &err, flags);
2424 if (n == NULL)
2425 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002426 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 return n;
Tim Petersfe2127d2001-07-16 05:37:24 +00002428}
2429
2430node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002431PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 int start, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 perrdetail err;
2435 node *n = PyParser_ParseStringFlagsFilename(str, filename,
2436 &_PyParser_Grammar, start, &err, flags);
2437 if (n == NULL)
2438 err_input(&err);
Victor Stinner7f2fee32011-04-05 00:39:01 +02002439 err_free(&err);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 return n;
Thomas Heller6b17abf2002-07-09 09:23:27 +00002441}
2442
2443node *
Martin v. Löwis95292d62002-12-11 14:04:59 +00002444PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
Thomas Heller6b17abf2002-07-09 09:23:27 +00002445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
Thomas Heller6b17abf2002-07-09 09:23:27 +00002447}
2448
Guido van Rossum66ebd912003-04-17 16:02:26 +00002449/* May want to move a more generalized form of this to parsetok.c or
2450 even parser modules. */
2451
2452void
Victor Stinner7f2fee32011-04-05 00:39:01 +02002453PyParser_ClearError(perrdetail *err)
2454{
2455 err_free(err);
2456}
2457
2458void
Guido van Rossum66ebd912003-04-17 16:02:26 +00002459PyParser_SetError(perrdetail *err)
2460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 err_input(err);
Guido van Rossum66ebd912003-04-17 16:02:26 +00002462}
2463
Victor Stinner7f2fee32011-04-05 00:39:01 +02002464static void
2465err_free(perrdetail *err)
2466{
2467 Py_CLEAR(err->filename);
2468}
2469
Guido van Rossuma110aa61994-08-29 12:50:44 +00002470/* Set the error appropriate to the given input error code (see errcode.h) */
2471
2472static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002473err_input(perrdetail *err)
Guido van Rossuma110aa61994-08-29 12:50:44 +00002474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PyObject *v, *w, *errtype, *errtext;
2476 PyObject *msg_obj = NULL;
2477 char *msg = NULL;
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02002478 int offset = err->offset;
Victor Stinner4c7c8c32010-10-16 13:14:10 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 errtype = PyExc_SyntaxError;
2481 switch (err->error) {
2482 case E_ERROR:
2483 return;
2484 case E_SYNTAX:
2485 errtype = PyExc_IndentationError;
2486 if (err->expected == INDENT)
2487 msg = "expected an indented block";
2488 else if (err->token == INDENT)
2489 msg = "unexpected indent";
2490 else if (err->token == DEDENT)
2491 msg = "unexpected unindent";
2492 else {
2493 errtype = PyExc_SyntaxError;
2494 msg = "invalid syntax";
2495 }
2496 break;
2497 case E_TOKEN:
2498 msg = "invalid token";
2499 break;
2500 case E_EOFS:
2501 msg = "EOF while scanning triple-quoted string literal";
2502 break;
2503 case E_EOLS:
2504 msg = "EOL while scanning string literal";
2505 break;
2506 case E_INTR:
2507 if (!PyErr_Occurred())
2508 PyErr_SetNone(PyExc_KeyboardInterrupt);
2509 goto cleanup;
2510 case E_NOMEM:
2511 PyErr_NoMemory();
2512 goto cleanup;
2513 case E_EOF:
2514 msg = "unexpected EOF while parsing";
2515 break;
2516 case E_TABSPACE:
2517 errtype = PyExc_TabError;
2518 msg = "inconsistent use of tabs and spaces in indentation";
2519 break;
2520 case E_OVERFLOW:
2521 msg = "expression too long";
2522 break;
2523 case E_DEDENT:
2524 errtype = PyExc_IndentationError;
2525 msg = "unindent does not match any outer indentation level";
2526 break;
2527 case E_TOODEEP:
2528 errtype = PyExc_IndentationError;
2529 msg = "too many levels of indentation";
2530 break;
2531 case E_DECODE: {
2532 PyObject *type, *value, *tb;
2533 PyErr_Fetch(&type, &value, &tb);
2534 msg = "unknown decode error";
2535 if (value != NULL)
2536 msg_obj = PyObject_Str(value);
2537 Py_XDECREF(type);
2538 Py_XDECREF(value);
2539 Py_XDECREF(tb);
2540 break;
2541 }
2542 case E_LINECONT:
2543 msg = "unexpected character after line continuation character";
2544 break;
Martin v. Löwis47383402007-08-15 07:32:56 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 case E_IDENTIFIER:
2547 msg = "invalid character in identifier";
2548 break;
Meador Ingefa21bf02012-01-19 01:08:41 -06002549 case E_BADSINGLE:
2550 msg = "multiple statements found while compiling a single statement";
2551 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 default:
2553 fprintf(stderr, "error=%d\n", err->error);
2554 msg = "unknown parsing error";
2555 break;
2556 }
2557 /* err->text may not be UTF-8 in case of decoding errors.
2558 Explicitly convert to an object. */
2559 if (!err->text) {
2560 errtext = Py_None;
2561 Py_INCREF(Py_None);
2562 } else {
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02002563 errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 "replace");
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02002565 if (errtext != NULL) {
2566 Py_ssize_t len = strlen(err->text);
2567 offset = (int)PyUnicode_GET_LENGTH(errtext);
2568 if (len != err->offset) {
2569 Py_DECREF(errtext);
2570 errtext = PyUnicode_DecodeUTF8(err->text, len,
2571 "replace");
2572 }
2573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02002575 v = Py_BuildValue("(OiiN)", err->filename,
Serhiy Storchaka65fd0592014-01-21 22:26:52 +02002576 err->lineno, offset, errtext);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 if (v != NULL) {
2578 if (msg_obj)
2579 w = Py_BuildValue("(OO)", msg_obj, v);
2580 else
2581 w = Py_BuildValue("(sO)", msg, v);
2582 } else
2583 w = NULL;
2584 Py_XDECREF(v);
2585 PyErr_SetObject(errtype, w);
2586 Py_XDECREF(w);
Georg Brandl3dbca812008-07-23 16:10:53 +00002587cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 Py_XDECREF(msg_obj);
2589 if (err->text != NULL) {
2590 PyObject_FREE(err->text);
2591 err->text = NULL;
2592 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002593}
2594
2595/* Print fatal error message and abort */
2596
2597void
Tim Peters7c321a82002-07-09 02:57:01 +00002598Py_FatalError(const char *msg)
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002599{
Victor Stinner024e37a2011-03-31 01:31:06 +02002600 const int fd = fileno(stderr);
2601 PyThreadState *tstate;
2602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 fprintf(stderr, "Fatal Python error: %s\n", msg);
2604 fflush(stderr); /* it helps in Windows debug build */
2605 if (PyErr_Occurred()) {
Victor Stinner55a5c782010-06-08 21:00:13 +00002606 PyErr_PrintEx(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 }
Victor Stinner024e37a2011-03-31 01:31:06 +02002608 else {
2609 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2610 if (tstate != NULL) {
2611 fputc('\n', stderr);
2612 fflush(stderr);
Victor Stinner7bba62f2011-05-07 12:43:00 +02002613 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
Victor Stinner024e37a2011-03-31 01:31:06 +02002614 }
Victor Stinnerd727e232011-04-01 12:13:55 +02002615 _PyFaulthandler_Fini();
Victor Stinner024e37a2011-03-31 01:31:06 +02002616 }
2617
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002618#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 {
2620 size_t len = strlen(msg);
2621 WCHAR* buffer;
2622 size_t i;
Martin v. Löwis5c88d812009-01-02 20:47:48 +00002623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 /* Convert the message to wchar_t. This uses a simple one-to-one
2625 conversion, assuming that the this error message actually uses ASCII
2626 only. If this ceases to be true, we will have to convert. */
2627 buffer = alloca( (len+1) * (sizeof *buffer));
2628 for( i=0; i<=len; ++i)
2629 buffer[i] = msg[i];
2630 OutputDebugStringW(L"Fatal Python error: ");
2631 OutputDebugStringW(buffer);
2632 OutputDebugStringW(L"\n");
2633 }
Guido van Rossum0ba35361998-08-13 13:33:16 +00002634#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 DebugBreak();
Guido van Rossuma44823b1995-03-14 15:01:17 +00002636#endif
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00002637#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 abort();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002639}
2640
2641/* Clean up and exit */
2642
Guido van Rossuma110aa61994-08-29 12:50:44 +00002643#ifdef WITH_THREAD
Guido van Rossum49b56061998-10-01 20:42:43 +00002644#include "pythread.h"
Guido van Rossumf9f2e821992-08-17 08:59:08 +00002645#endif
2646
Collin Winter670e6922007-03-21 02:57:17 +00002647static void (*pyexitfunc)(void) = NULL;
2648/* For the atexit module. */
2649void _Py_PyAtExit(void (*func)(void))
2650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 pyexitfunc = func;
Collin Winter670e6922007-03-21 02:57:17 +00002652}
2653
2654static void
2655call_py_exitfuncs(void)
2656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 if (pyexitfunc == NULL)
2658 return;
Collin Winter670e6922007-03-21 02:57:17 +00002659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 (*pyexitfunc)();
2661 PyErr_Clear();
Collin Winter670e6922007-03-21 02:57:17 +00002662}
2663
Antoine Pitrou011bd622009-10-20 21:52:47 +00002664/* Wait until threading._shutdown completes, provided
2665 the threading module was imported in the first place.
2666 The shutdown routine will wait until all non-daemon
2667 "threading" threads have completed. */
2668static void
2669wait_for_thread_shutdown(void)
2670{
2671#ifdef WITH_THREAD
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002672 _Py_IDENTIFIER(_shutdown);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 PyObject *result;
2674 PyThreadState *tstate = PyThreadState_GET();
2675 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2676 "threading");
2677 if (threading == NULL) {
2678 /* threading not imported */
2679 PyErr_Clear();
2680 return;
2681 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002682 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 if (result == NULL) {
2684 PyErr_WriteUnraisable(threading);
2685 }
2686 else {
2687 Py_DECREF(result);
2688 }
2689 Py_DECREF(threading);
Antoine Pitrou011bd622009-10-20 21:52:47 +00002690#endif
2691}
2692
Guido van Rossum2dcfc961998-10-01 16:01:57 +00002693#define NEXITFUNCS 32
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002694static void (*exitfuncs[NEXITFUNCS])(void);
Guido van Rossum1662dd51994-09-07 14:38:28 +00002695static int nexitfuncs = 0;
2696
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002697int Py_AtExit(void (*func)(void))
Guido van Rossum1662dd51994-09-07 14:38:28 +00002698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 if (nexitfuncs >= NEXITFUNCS)
2700 return -1;
2701 exitfuncs[nexitfuncs++] = func;
2702 return 0;
Guido van Rossum1662dd51994-09-07 14:38:28 +00002703}
2704
Guido van Rossumcc283f51997-08-05 02:22:03 +00002705static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002706call_ll_exitfuncs(void)
Guido van Rossumcc283f51997-08-05 02:22:03 +00002707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 while (nexitfuncs > 0)
2709 (*exitfuncs[--nexitfuncs])();
Guido van Rossum25ce5661997-08-02 03:10:38 +00002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 fflush(stdout);
2712 fflush(stderr);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002713}
2714
2715void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002716Py_Exit(int sts)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 Py_Finalize();
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 exit(sts);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00002721}
2722
Guido van Rossumf1dc5661993-07-05 10:31:29 +00002723static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002724initsigs(void)
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002725{
Guido van Rossuma110aa61994-08-29 12:50:44 +00002726#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 PyOS_setsig(SIGPIPE, SIG_IGN);
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002728#endif
Guido van Rossum70d893a2001-08-16 08:21:42 +00002729#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 PyOS_setsig(SIGXFZ, SIG_IGN);
Guido van Rossum70d893a2001-08-16 08:21:42 +00002731#endif
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002732#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 PyOS_setsig(SIGXFSZ, SIG_IGN);
Jeremy Hylton1b0bf9b2002-04-23 20:31:01 +00002734#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 PyOS_InitInterrupts(); /* May imply initsignal() */
Victor Stinnerd786ad52013-07-21 13:25:51 +02002736 if (PyErr_Occurred()) {
2737 Py_FatalError("Py_Initialize: can't import signal");
2738 }
Guido van Rossuma9e7dc11992-10-18 18:53:57 +00002739}
2740
Guido van Rossum7433b121997-02-14 19:45:36 +00002741
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002742/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2743 *
2744 * All of the code in this function must only use async-signal-safe functions,
2745 * listed at `man 7 signal` or
2746 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2747 */
2748void
2749_Py_RestoreSignals(void)
2750{
2751#ifdef SIGPIPE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 PyOS_setsig(SIGPIPE, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002753#endif
2754#ifdef SIGXFZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 PyOS_setsig(SIGXFZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002756#endif
2757#ifdef SIGXFSZ
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 PyOS_setsig(SIGXFSZ, SIG_DFL);
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002759#endif
2760}
2761
2762
Guido van Rossum7433b121997-02-14 19:45:36 +00002763/*
2764 * The file descriptor fd is considered ``interactive'' if either
2765 * a) isatty(fd) is TRUE, or
2766 * b) the -i flag was given, and the filename associated with
2767 * the descriptor is NULL or "<stdin>" or "???".
2768 */
2769int
Martin v. Löwis95292d62002-12-11 14:04:59 +00002770Py_FdIsInteractive(FILE *fp, const char *filename)
Guido van Rossum7433b121997-02-14 19:45:36 +00002771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (isatty((int)fileno(fp)))
2773 return 1;
2774 if (!Py_InteractiveFlag)
2775 return 0;
2776 return (filename == NULL) ||
2777 (strcmp(filename, "<stdin>") == 0) ||
2778 (strcmp(filename, "???") == 0);
Guido van Rossum7433b121997-02-14 19:45:36 +00002779}
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002780
2781
Tim Petersd08e3822003-04-17 15:24:21 +00002782#if defined(USE_STACKCHECK)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002783#if defined(WIN32) && defined(_MSC_VER)
2784
2785/* Stack checking for Microsoft C */
2786
2787#include <malloc.h>
2788#include <excpt.h>
2789
Fred Drakee8de31c2000-08-31 05:38:39 +00002790/*
2791 * Return non-zero when we run out of memory on the stack; zero otherwise.
2792 */
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002793int
Fred Drake399739f2000-08-31 05:52:44 +00002794PyOS_CheckStack(void)
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 __try {
2797 /* alloca throws a stack overflow exception if there's
2798 not enough space left on the stack */
2799 alloca(PYOS_STACK_MARGIN * sizeof(void*));
2800 return 0;
2801 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
2802 EXCEPTION_EXECUTE_HANDLER :
2803 EXCEPTION_CONTINUE_SEARCH) {
2804 int errcode = _resetstkoflw();
2805 if (errcode == 0)
2806 {
2807 Py_FatalError("Could not reset the stack!");
2808 }
2809 }
2810 return 1;
Fredrik Lundh2f15b252000-08-27 19:15:31 +00002811}
2812
2813#endif /* WIN32 && _MSC_VER */
2814
2815/* Alternate implementations can be added here... */
2816
2817#endif /* USE_STACKCHECK */
Guido van Rossum6f256182000-09-16 16:32:19 +00002818
2819
2820/* Wrappers around sigaction() or signal(). */
2821
2822PyOS_sighandler_t
2823PyOS_getsig(int sig)
2824{
2825#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 struct sigaction context;
2827 if (sigaction(sig, NULL, &context) == -1)
2828 return SIG_ERR;
2829 return context.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002830#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 PyOS_sighandler_t handler;
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002832/* Special signal handling for the secure CRT in Visual Studio 2005 */
2833#if defined(_MSC_VER) && _MSC_VER >= 1400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 switch (sig) {
2835 /* Only these signals are valid */
2836 case SIGINT:
2837 case SIGILL:
2838 case SIGFPE:
2839 case SIGSEGV:
2840 case SIGTERM:
2841 case SIGBREAK:
2842 case SIGABRT:
2843 break;
2844 /* Don't call signal() with other values or it will assert */
2845 default:
2846 return SIG_ERR;
2847 }
Martin v. Löwisb45b3152005-11-28 17:34:23 +00002848#endif /* _MSC_VER && _MSC_VER >= 1400 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 handler = signal(sig, SIG_IGN);
2850 if (handler != SIG_ERR)
2851 signal(sig, handler);
2852 return handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002853#endif
2854}
2855
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00002856/*
2857 * All of the code in this function must only use async-signal-safe functions,
2858 * listed at `man 7 signal` or
2859 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2860 */
Guido van Rossum6f256182000-09-16 16:32:19 +00002861PyOS_sighandler_t
2862PyOS_setsig(int sig, PyOS_sighandler_t handler)
2863{
2864#ifdef HAVE_SIGACTION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 /* Some code in Modules/signalmodule.c depends on sigaction() being
2866 * used here if HAVE_SIGACTION is defined. Fix that if this code
2867 * changes to invalidate that assumption.
2868 */
2869 struct sigaction context, ocontext;
2870 context.sa_handler = handler;
2871 sigemptyset(&context.sa_mask);
2872 context.sa_flags = 0;
2873 if (sigaction(sig, &context, &ocontext) == -1)
2874 return SIG_ERR;
2875 return ocontext.sa_handler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002876#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 PyOS_sighandler_t oldhandler;
2878 oldhandler = signal(sig, handler);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002879#ifdef HAVE_SIGINTERRUPT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 siginterrupt(sig, 1);
Anthony Baxter9ceaa722004-10-13 14:48:50 +00002881#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 return oldhandler;
Guido van Rossum6f256182000-09-16 16:32:19 +00002883#endif
2884}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885
2886/* Deprecated C API functions still provided for binary compatiblity */
2887
2888#undef PyParser_SimpleParseFile
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002889PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893}
2894
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002895#undef PyParser_SimpleParseString
2896PyAPI_FUNC(node *)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897PyParser_SimpleParseString(const char *str, int start)
2898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 return PyParser_SimpleParseStringFlags(str, start, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002901
2902#undef PyRun_AnyFile
2903PyAPI_FUNC(int)
2904PyRun_AnyFile(FILE *fp, const char *name)
2905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002907}
2908
2909#undef PyRun_AnyFileEx
2910PyAPI_FUNC(int)
2911PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002914}
2915
2916#undef PyRun_AnyFileFlags
2917PyAPI_FUNC(int)
2918PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 return PyRun_AnyFileExFlags(fp, name, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002921}
2922
2923#undef PyRun_File
2924PyAPI_FUNC(PyObject *)
2925PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002928}
2929
2930#undef PyRun_FileEx
2931PyAPI_FUNC(PyObject *)
2932PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002935}
2936
2937#undef PyRun_FileFlags
2938PyAPI_FUNC(PyObject *)
2939PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 PyCompilerFlags *flags)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002943}
2944
2945#undef PyRun_SimpleFile
2946PyAPI_FUNC(int)
2947PyRun_SimpleFile(FILE *f, const char *p)
2948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002950}
2951
2952#undef PyRun_SimpleFileEx
2953PyAPI_FUNC(int)
2954PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 return PyRun_SimpleFileExFlags(f, p, c, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002957}
2958
2959
2960#undef PyRun_String
2961PyAPI_FUNC(PyObject *)
2962PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 return PyRun_StringFlags(str, s, g, l, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002965}
2966
2967#undef PyRun_SimpleString
2968PyAPI_FUNC(int)
2969PyRun_SimpleString(const char *s)
2970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 return PyRun_SimpleStringFlags(s, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002972}
2973
2974#undef Py_CompileString
2975PyAPI_FUNC(PyObject *)
2976Py_CompileString(const char *str, const char *p, int s)
2977{
Georg Brandl8334fd92010-12-04 10:26:46 +00002978 return Py_CompileStringExFlags(str, p, s, NULL, -1);
2979}
2980
2981#undef Py_CompileStringFlags
2982PyAPI_FUNC(PyObject *)
2983Py_CompileStringFlags(const char *str, const char *p, int s,
2984 PyCompilerFlags *flags)
2985{
2986 return Py_CompileStringExFlags(str, p, s, flags, -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002987}
2988
2989#undef PyRun_InteractiveOne
2990PyAPI_FUNC(int)
2991PyRun_InteractiveOne(FILE *f, const char *p)
2992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 return PyRun_InteractiveOneFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002994}
2995
2996#undef PyRun_InteractiveLoop
2997PyAPI_FUNC(int)
2998PyRun_InteractiveLoop(FILE *f, const char *p)
2999{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 return PyRun_InteractiveLoopFlags(f, p, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003001}
3002
3003#ifdef __cplusplus
3004}
3005#endif