pythonrun.c revision 9ed77358d618e4c65fbe5df67a87618be29fc391
1
2/* Python interpreter top-level routines, including init/exit */
3
4#include "Python.h"
5
6#include "Python-ast.h"
7#undef Yield /* undefine macro conflicting with winbase.h */
8#include "grammar.h"
9#include "node.h"
10#include "token.h"
11#include "parsetok.h"
12#include "errcode.h"
13#include "code.h"
14#include "compile.h"
15#include "symtable.h"
16#include "pyarena.h"
17#include "ast.h"
18#include "eval.h"
19#include "marshal.h"
20
21#ifdef HAVE_SIGNAL_H
22#include <signal.h>
23#endif
24
25#ifdef HAVE_LANGINFO_H
26#include <locale.h>
27#include <langinfo.h>
28#endif
29
30#ifdef MS_WINDOWS
31#undef BYTE
32#include "windows.h"
33#endif
34
35#ifndef Py_REF_DEBUG
36#define PRINT_TOTAL_REFS()
37#else /* Py_REF_DEBUG */
38#define PRINT_TOTAL_REFS() fprintf(stderr,				\
39				   "[%" PY_FORMAT_SIZE_T "d refs]\n",	\
40				   _Py_GetRefTotal())
41#endif
42
43#ifdef __cplusplus
44extern "C" {
45#endif
46
47extern char *Py_GetPath(void);
48
49extern grammar _PyParser_Grammar; /* From graminit.c */
50
51/* Forward */
52static void initmain(void);
53static void initsite(void);
54static int initstdio(void);
55static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
56			  PyCompilerFlags *, PyArena *);
57static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
58			      PyCompilerFlags *);
59static void err_input(perrdetail *);
60static void initsigs(void);
61static void call_py_exitfuncs(void);
62static void call_ll_exitfuncs(void);
63extern void _PyUnicode_Init(void);
64extern void _PyUnicode_Fini(void);
65extern int _PyLong_Init(void);
66extern void PyLong_Fini(void);
67
68#ifdef WITH_THREAD
69extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
70extern void _PyGILState_Fini(void);
71#endif /* WITH_THREAD */
72
73int Py_DebugFlag; /* Needed by parser.c */
74int Py_VerboseFlag; /* Needed by import.c */
75int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
76int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
77int Py_NoSiteFlag; /* Suppress 'import site' */
78int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
79int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
80int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
81int Py_FrozenFlag; /* Needed by getpath.c */
82int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
83
84/* Reference to 'warnings' module, to avoid importing it
85   on the fly when the import lock may be held.  See 683658/771097
86*/
87static PyObject *warnings_module = NULL;
88
89/* Returns a borrowed reference to the 'warnings' module, or NULL.
90   If the module is returned, it is guaranteed to have been obtained
91   without acquiring the import lock
92*/
93PyObject *PyModule_GetWarningsModule(void)
94{
95	PyObject *typ, *val, *tb;
96	PyObject *all_modules;
97	/* If we managed to get the module at init time, just use it */
98	if (warnings_module)
99		return warnings_module;
100	/* If it wasn't available at init time, it may be available
101	   now in sys.modules (common scenario is frozen apps: import
102	   at init time fails, but the frozen init code sets up sys.path
103	   correctly, then does an implicit import of warnings for us
104	*/
105	/* Save and restore any exceptions */
106	PyErr_Fetch(&typ, &val, &tb);
107
108	all_modules = PySys_GetObject("modules");
109	if (all_modules) {
110		warnings_module = PyDict_GetItemString(all_modules, "warnings");
111		/* We keep a ref in the global */
112		Py_XINCREF(warnings_module);
113	}
114	PyErr_Restore(typ, val, tb);
115	return warnings_module;
116}
117
118static int initialized = 0;
119
120/* API to access the initialized flag -- useful for esoteric use */
121
122int
123Py_IsInitialized(void)
124{
125	return initialized;
126}
127
128/* Global initializations.  Can be undone by Py_Finalize().  Don't
129   call this twice without an intervening Py_Finalize() call.  When
130   initializations fail, a fatal error is issued and the function does
131   not return.  On return, the first thread and interpreter state have
132   been created.
133
134   Locking: you must hold the interpreter lock while calling this.
135   (If the lock has not yet been initialized, that's equivalent to
136   having the lock, but you cannot use multiple threads.)
137
138*/
139
140static int
141add_flag(int flag, const char *envs)
142{
143	int env = atoi(envs);
144	if (flag < env)
145		flag = env;
146	if (flag < 1)
147		flag = 1;
148	return flag;
149}
150
151void
152Py_InitializeEx(int install_sigs)
153{
154	PyInterpreterState *interp;
155	PyThreadState *tstate;
156	PyObject *bimod, *sysmod, *pstderr;
157	char *p;
158#if defined(HAVE_LANGINFO_H) && defined(CODESET)
159	char *codeset;
160#endif
161	extern void _Py_ReadyTypes(void);
162
163	if (initialized)
164		return;
165	initialized = 1;
166
167#ifdef HAVE_SETLOCALE
168	/* Set up the LC_CTYPE locale, so we can obtain
169	   the locale's charset without having to switch
170	   locales. */
171	setlocale(LC_CTYPE, "");
172#endif
173
174	if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
175		Py_DebugFlag = add_flag(Py_DebugFlag, p);
176	if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
177		Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
178	if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
179		Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
180	if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
181		Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
182
183	interp = PyInterpreterState_New();
184	if (interp == NULL)
185		Py_FatalError("Py_Initialize: can't make first interpreter");
186
187	tstate = PyThreadState_New(interp);
188	if (tstate == NULL)
189		Py_FatalError("Py_Initialize: can't make first thread");
190	(void) PyThreadState_Swap(tstate);
191
192	_Py_ReadyTypes();
193
194	if (!_PyFrame_Init())
195		Py_FatalError("Py_Initialize: can't init frames");
196
197	if (!_PyLong_Init())
198		Py_FatalError("Py_Initialize: can't init longs");
199
200	if (!PyBytes_Init())
201		Py_FatalError("Py_Initialize: can't init bytes");
202
203	_PyFloat_Init();
204
205	interp->modules = PyDict_New();
206	if (interp->modules == NULL)
207		Py_FatalError("Py_Initialize: can't make modules dictionary");
208	interp->modules_reloading = PyDict_New();
209	if (interp->modules_reloading == NULL)
210		Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
211
212	/* Init Unicode implementation; relies on the codec registry */
213	_PyUnicode_Init();
214
215	bimod = _PyBuiltin_Init();
216	if (bimod == NULL)
217		Py_FatalError("Py_Initialize: can't initialize builtins modules");
218	interp->builtins = PyModule_GetDict(bimod);
219	if (interp->builtins == NULL)
220		Py_FatalError("Py_Initialize: can't initialize builtins dict");
221	Py_INCREF(interp->builtins);
222
223	/* initialize builtin exceptions */
224	_PyExc_Init();
225
226	sysmod = _PySys_Init();
227	if (sysmod == NULL)
228		Py_FatalError("Py_Initialize: can't initialize sys");
229	interp->sysdict = PyModule_GetDict(sysmod);
230	if (interp->sysdict == NULL)
231		Py_FatalError("Py_Initialize: can't initialize sys dict");
232	Py_INCREF(interp->sysdict);
233	_PyImport_FixupExtension("sys", "sys");
234	PySys_SetPath(Py_GetPath());
235	PyDict_SetItemString(interp->sysdict, "modules",
236			     interp->modules);
237
238	/* Set up a preliminary stderr printer until we have enough
239	   infrastructure for the io module in place. */
240	pstderr = PyFile_NewStdPrinter(fileno(stderr));
241	if (pstderr == NULL)
242		Py_FatalError("Py_Initialize: can't set preliminary stderr");
243	PySys_SetObject("stderr", pstderr);
244	PySys_SetObject("__stderr__", pstderr);
245
246	_PyImport_Init();
247
248	/* phase 2 of builtins */
249	_PyImport_FixupExtension("builtins", "builtins");
250
251	_PyImportHooks_Init();
252
253	if (install_sigs)
254		initsigs(); /* Signal handling stuff, including initintr() */
255
256	initmain(); /* Module __main__ */
257	if (initstdio() < 0)
258		Py_FatalError(
259		    "Py_Initialize: can't initialize sys standard streams");
260	if (!Py_NoSiteFlag)
261		initsite(); /* Module site */
262
263	/* auto-thread-state API, if available */
264#ifdef WITH_THREAD
265	_PyGILState_Init(interp, tstate);
266#endif /* WITH_THREAD */
267
268	warnings_module = PyImport_ImportModule("warnings");
269	if (!warnings_module) {
270		PyErr_Clear();
271	}
272	else {
273		PyObject *o;
274		char *action[8];
275
276		if (Py_BytesWarningFlag > 1)
277			*action = "error";
278		else if (Py_BytesWarningFlag)
279			*action = "default";
280		else
281			*action = "ignore";
282
283		o = PyObject_CallMethod(warnings_module,
284					"simplefilter", "sO",
285					*action, PyExc_BytesWarning);
286		if (o == NULL)
287			Py_FatalError("Py_Initialize: can't initialize"
288				      "warning filter for BytesWarning.");
289		Py_DECREF(o);
290        }
291
292#if defined(HAVE_LANGINFO_H) && defined(CODESET)
293	/* On Unix, set the file system encoding according to the
294	   user's preference, if the CODESET names a well-known
295	   Python codec, and Py_FileSystemDefaultEncoding isn't
296	   initialized by other means. Also set the encoding of
297	   stdin and stdout if these are terminals.  */
298
299	codeset = nl_langinfo(CODESET);
300	if (codeset && *codeset) {
301		PyObject *enc = PyCodec_Encoder(codeset);
302		if (enc) {
303			codeset = strdup(codeset);
304			Py_DECREF(enc);
305		} else {
306			codeset = NULL;
307			PyErr_Clear();
308		}
309	} else
310		codeset = NULL;
311
312	if (codeset) {
313		if (!Py_FileSystemDefaultEncoding)
314			Py_FileSystemDefaultEncoding = codeset;
315		else
316			free(codeset);
317	}
318#endif
319}
320
321void
322Py_Initialize(void)
323{
324	Py_InitializeEx(1);
325}
326
327
328#ifdef COUNT_ALLOCS
329extern void dump_counts(FILE*);
330#endif
331
332/* Flush stdout and stderr */
333
334static void
335flush_std_files(void)
336{
337	PyObject *fout = PySys_GetObject("stdout");
338	PyObject *ferr = PySys_GetObject("stderr");
339	PyObject *tmp;
340
341	if (fout != NULL && fout != Py_None) {
342		tmp = PyObject_CallMethod(fout, "flush", "");
343		if (tmp == NULL)
344			PyErr_Clear();
345		else
346			Py_DECREF(tmp);
347	}
348
349	if (ferr != NULL || ferr != Py_None) {
350		tmp = PyObject_CallMethod(ferr, "flush", "");
351		if (tmp == NULL)
352			PyErr_Clear();
353		else
354			Py_DECREF(tmp);
355	}
356}
357
358/* Undo the effect of Py_Initialize().
359
360   Beware: if multiple interpreter and/or thread states exist, these
361   are not wiped out; only the current thread and interpreter state
362   are deleted.  But since everything else is deleted, those other
363   interpreter and thread states should no longer be used.
364
365   (XXX We should do better, e.g. wipe out all interpreters and
366   threads.)
367
368   Locking: as above.
369
370*/
371
372void
373Py_Finalize(void)
374{
375	PyInterpreterState *interp;
376	PyThreadState *tstate;
377
378	if (!initialized)
379		return;
380
381	/* The interpreter is still entirely intact at this point, and the
382	 * exit funcs may be relying on that.  In particular, if some thread
383	 * or exit func is still waiting to do an import, the import machinery
384	 * expects Py_IsInitialized() to return true.  So don't say the
385	 * interpreter is uninitialized until after the exit funcs have run.
386	 * Note that Threading.py uses an exit func to do a join on all the
387	 * threads created thru it, so this also protects pending imports in
388	 * the threads created via Threading.
389	 */
390	call_py_exitfuncs();
391	initialized = 0;
392
393	/* Flush stdout+stderr */
394	flush_std_files();
395
396	/* Get current thread state and interpreter pointer */
397	tstate = PyThreadState_GET();
398	interp = tstate->interp;
399
400	/* Disable signal handling */
401	PyOS_FiniInterrupts();
402
403	/* drop module references we saved */
404	Py_XDECREF(warnings_module);
405	warnings_module = NULL;
406
407	/* Clear type lookup cache */
408	PyType_ClearCache();
409
410	/* Collect garbage.  This may call finalizers; it's nice to call these
411	 * before all modules are destroyed.
412	 * XXX If a __del__ or weakref callback is triggered here, and tries to
413	 * XXX import a module, bad things can happen, because Python no
414	 * XXX longer believes it's initialized.
415	 * XXX     Fatal Python error: Interpreter not initialized (version mismatch?)
416	 * XXX is easy to provoke that way.  I've also seen, e.g.,
417	 * XXX     Exception exceptions.ImportError: 'No module named sha'
418	 * XXX         in <function callback at 0x008F5718> ignored
419	 * XXX but I'm unclear on exactly how that one happens.  In any case,
420	 * XXX I haven't seen a real-life report of either of these.
421	 */
422	PyGC_Collect();
423#ifdef COUNT_ALLOCS
424	/* With COUNT_ALLOCS, it helps to run GC multiple times:
425	   each collection might release some types from the type
426	   list, so they become garbage. */
427	while (PyGC_Collect() > 0)
428		/* nothing */;
429#endif
430
431	/* Destroy all modules */
432	PyImport_Cleanup();
433
434	/* Flush stdout+stderr (again, in case more was printed) */
435	flush_std_files();
436
437	/* Collect final garbage.  This disposes of cycles created by
438	 * new-style class definitions, for example.
439	 * XXX This is disabled because it caused too many problems.  If
440	 * XXX a __del__ or weakref callback triggers here, Python code has
441	 * XXX a hard time running, because even the sys module has been
442	 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
443	 * XXX One symptom is a sequence of information-free messages
444	 * XXX coming from threads (if a __del__ or callback is invoked,
445	 * XXX other threads can execute too, and any exception they encounter
446	 * XXX triggers a comedy of errors as subsystem after subsystem
447	 * XXX fails to find what it *expects* to find in sys to help report
448	 * XXX the exception and consequent unexpected failures).  I've also
449	 * XXX seen segfaults then, after adding print statements to the
450	 * XXX Python code getting called.
451	 */
452#if 0
453	PyGC_Collect();
454#endif
455
456	/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
457	_PyImport_Fini();
458
459	/* Debugging stuff */
460#ifdef COUNT_ALLOCS
461	dump_counts(stdout);
462#endif
463
464	PRINT_TOTAL_REFS();
465
466#ifdef Py_TRACE_REFS
467	/* Display all objects still alive -- this can invoke arbitrary
468	 * __repr__ overrides, so requires a mostly-intact interpreter.
469	 * Alas, a lot of stuff may still be alive now that will be cleaned
470	 * up later.
471	 */
472	if (Py_GETENV("PYTHONDUMPREFS"))
473		_Py_PrintReferences(stderr);
474#endif /* Py_TRACE_REFS */
475
476	/* Clear interpreter state */
477	PyInterpreterState_Clear(interp);
478
479	/* Now we decref the exception classes.  After this point nothing
480	   can raise an exception.  That's okay, because each Fini() method
481	   below has been checked to make sure no exceptions are ever
482	   raised.
483	*/
484
485	_PyExc_Fini();
486
487	/* Cleanup auto-thread-state */
488#ifdef WITH_THREAD
489	_PyGILState_Fini();
490#endif /* WITH_THREAD */
491
492	/* Delete current thread */
493	PyThreadState_Swap(NULL);
494	PyInterpreterState_Delete(interp);
495
496	/* Sundry finalizers */
497	PyMethod_Fini();
498	PyFrame_Fini();
499	PyCFunction_Fini();
500	PyTuple_Fini();
501	PyList_Fini();
502	PySet_Fini();
503	PyString_Fini();
504	PyBytes_Fini();
505	PyLong_Fini();
506	PyFloat_Fini();
507	PyDict_Fini();
508
509	/* Cleanup Unicode implementation */
510	_PyUnicode_Fini();
511
512	/* reset file system default encoding */
513	if (!Py_HasFileSystemDefaultEncoding) {
514		free((char*)Py_FileSystemDefaultEncoding);
515		Py_FileSystemDefaultEncoding = NULL;
516	}
517
518	/* XXX Still allocated:
519	   - various static ad-hoc pointers to interned strings
520	   - int and float free list blocks
521	   - whatever various modules and libraries allocate
522	*/
523
524	PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
525
526#ifdef Py_TRACE_REFS
527	/* Display addresses (& refcnts) of all objects still alive.
528	 * An address can be used to find the repr of the object, printed
529	 * above by _Py_PrintReferences.
530	 */
531	if (Py_GETENV("PYTHONDUMPREFS"))
532		_Py_PrintReferenceAddresses(stderr);
533#endif /* Py_TRACE_REFS */
534#ifdef PYMALLOC_DEBUG
535	if (Py_GETENV("PYTHONMALLOCSTATS"))
536		_PyObject_DebugMallocStats();
537#endif
538
539	call_ll_exitfuncs();
540}
541
542/* Create and initialize a new interpreter and thread, and return the
543   new thread.  This requires that Py_Initialize() has been called
544   first.
545
546   Unsuccessful initialization yields a NULL pointer.  Note that *no*
547   exception information is available even in this case -- the
548   exception information is held in the thread, and there is no
549   thread.
550
551   Locking: as above.
552
553*/
554
555PyThreadState *
556Py_NewInterpreter(void)
557{
558	PyInterpreterState *interp;
559	PyThreadState *tstate, *save_tstate;
560	PyObject *bimod, *sysmod;
561
562	if (!initialized)
563		Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
564
565	interp = PyInterpreterState_New();
566	if (interp == NULL)
567		return NULL;
568
569	tstate = PyThreadState_New(interp);
570	if (tstate == NULL) {
571		PyInterpreterState_Delete(interp);
572		return NULL;
573	}
574
575	save_tstate = PyThreadState_Swap(tstate);
576
577	/* XXX The following is lax in error checking */
578
579	interp->modules = PyDict_New();
580	interp->modules_reloading = PyDict_New();
581
582	bimod = _PyImport_FindExtension("builtins", "builtins");
583	if (bimod != NULL) {
584		interp->builtins = PyModule_GetDict(bimod);
585		if (interp->builtins == NULL)
586			goto handle_error;
587		Py_INCREF(interp->builtins);
588	}
589	sysmod = _PyImport_FindExtension("sys", "sys");
590	if (bimod != NULL && sysmod != NULL) {
591		interp->sysdict = PyModule_GetDict(sysmod);
592		if (interp->sysdict == NULL)
593			goto handle_error;
594		Py_INCREF(interp->sysdict);
595		PySys_SetPath(Py_GetPath());
596		PyDict_SetItemString(interp->sysdict, "modules",
597				     interp->modules);
598		_PyImportHooks_Init();
599		initmain();
600		if (!Py_NoSiteFlag)
601			initsite();
602	}
603
604	if (!PyErr_Occurred())
605		return tstate;
606
607handle_error:
608	/* Oops, it didn't work.  Undo it all. */
609
610	PyErr_Print();
611	PyThreadState_Clear(tstate);
612	PyThreadState_Swap(save_tstate);
613	PyThreadState_Delete(tstate);
614	PyInterpreterState_Delete(interp);
615
616	return NULL;
617}
618
619/* Delete an interpreter and its last thread.  This requires that the
620   given thread state is current, that the thread has no remaining
621   frames, and that it is its interpreter's only remaining thread.
622   It is a fatal error to violate these constraints.
623
624   (Py_Finalize() doesn't have these constraints -- it zaps
625   everything, regardless.)
626
627   Locking: as above.
628
629*/
630
631void
632Py_EndInterpreter(PyThreadState *tstate)
633{
634	PyInterpreterState *interp = tstate->interp;
635
636	if (tstate != PyThreadState_GET())
637		Py_FatalError("Py_EndInterpreter: thread is not current");
638	if (tstate->frame != NULL)
639		Py_FatalError("Py_EndInterpreter: thread still has a frame");
640	if (tstate != interp->tstate_head || tstate->next != NULL)
641		Py_FatalError("Py_EndInterpreter: not the last thread");
642
643	PyImport_Cleanup();
644	PyInterpreterState_Clear(interp);
645	PyThreadState_Swap(NULL);
646	PyInterpreterState_Delete(interp);
647}
648
649static char *progname = "python";
650
651void
652Py_SetProgramName(char *pn)
653{
654	if (pn && *pn)
655		progname = pn;
656}
657
658char *
659Py_GetProgramName(void)
660{
661	return progname;
662}
663
664static char *default_home = NULL;
665
666void
667Py_SetPythonHome(char *home)
668{
669	default_home = home;
670}
671
672char *
673Py_GetPythonHome(void)
674{
675	char *home = default_home;
676	if (home == NULL && !Py_IgnoreEnvironmentFlag)
677		home = Py_GETENV("PYTHONHOME");
678	return home;
679}
680
681/* Create __main__ module */
682
683static void
684initmain(void)
685{
686	PyObject *m, *d;
687	m = PyImport_AddModule("__main__");
688	if (m == NULL)
689		Py_FatalError("can't create __main__ module");
690	d = PyModule_GetDict(m);
691	if (PyDict_GetItemString(d, "__builtins__") == NULL) {
692		PyObject *bimod = PyImport_ImportModule("builtins");
693		if (bimod == NULL ||
694		    PyDict_SetItemString(d, "__builtins__", bimod) != 0)
695			Py_FatalError("can't add __builtins__ to __main__");
696		Py_DECREF(bimod);
697	}
698}
699
700/* Import the site module (not into __main__ though) */
701
702static void
703initsite(void)
704{
705	PyObject *m, *f;
706	m = PyImport_ImportModule("site");
707	if (m == NULL) {
708		f = PySys_GetObject("stderr");
709		if (f == NULL || f == Py_None)
710			return;
711		if (Py_VerboseFlag) {
712			PyFile_WriteString(
713				"'import site' failed; traceback:\n", f);
714			PyErr_Print();
715		}
716		else {
717			PyFile_WriteString(
718			  "'import site' failed; use -v for traceback\n", f);
719			PyErr_Clear();
720		}
721	}
722	else {
723		Py_DECREF(m);
724	}
725}
726
727/* Initialize sys.stdin, stdout, stderr and builtins.open */
728static int
729initstdio(void)
730{
731	PyObject *iomod = NULL, *wrapper;
732	PyObject *bimod = NULL;
733	PyObject *m;
734	PyObject *std = NULL;
735	int status = 0, fd;
736	PyObject * encoding_attr;
737
738	/* Hack to avoid a nasty recursion issue when Python is invoked
739	   in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
740	if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
741		goto error;
742	}
743	Py_DECREF(m);
744
745	if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
746		goto error;
747	}
748	Py_DECREF(m);
749
750	if (!(bimod = PyImport_ImportModule("builtins"))) {
751		goto error;
752	}
753
754	if (!(iomod = PyImport_ImportModule("io"))) {
755		goto error;
756	}
757	if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
758		goto error;
759	}
760
761	/* Set builtins.open */
762	if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
763		goto error;
764	}
765
766	/* Set sys.stdin */
767	fd = fileno(stdin);
768	/* Under some conditions stdin, stdout and stderr may not be connected
769	 * and fileno() may point to an invalid file descriptor. For example
770	 * GUI apps don't have valid standard streams by default.
771	 */
772	if (fd < 0) {
773#ifdef MS_WINDOWS
774		std = Py_None;
775		Py_INCREF(std);
776#else
777		goto error;
778#endif
779	}
780	else {
781		if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
782					  "\n", 0))) {
783			goto error;
784		}
785	} /* if (fd < 0) */
786	PySys_SetObject("__stdin__", std);
787	PySys_SetObject("stdin", std);
788	Py_DECREF(std);
789
790	/* Set sys.stdout */
791	fd = fileno(stdout);
792	if (fd < 0) {
793#ifdef MS_WINDOWS
794		std = Py_None;
795		Py_INCREF(std);
796#else
797		goto error;
798#endif
799	}
800	else {
801		if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
802					  "\n", 0))) {
803			goto error;
804		}
805	} /* if (fd < 0) */
806	PySys_SetObject("__stdout__", std);
807	PySys_SetObject("stdout", std);
808	Py_DECREF(std);
809
810#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
811	/* Set sys.stderr, replaces the preliminary stderr */
812	fd = fileno(stderr);
813	if (fd < 0) {
814#ifdef MS_WINDOWS
815		std = Py_None;
816		Py_INCREF(std);
817#else
818		goto error;
819#endif
820	}
821	else {
822		if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
823					  "\n", 0))) {
824			goto error;
825		}
826	} /* if (fd < 0) */
827
828	/* Same as hack above, pre-import stderr's codec to avoid recursion
829	   when import.c tries to write to stderr in verbose mode. */
830	encoding_attr = PyObject_GetAttrString(std, "encoding");
831	if (encoding_attr != NULL) {
832		const char * encoding;
833		encoding = PyUnicode_AsString(encoding_attr);
834		if (encoding != NULL) {
835			_PyCodec_Lookup(encoding);
836		}
837	}
838	PyErr_Clear();  /* Not a fatal error if codec isn't available */
839
840	PySys_SetObject("__stderr__", std);
841	PySys_SetObject("stderr", std);
842	Py_DECREF(std);
843#endif
844
845	if (0) {
846  error:
847		status = -1;
848	}
849
850	Py_XDECREF(bimod);
851	Py_XDECREF(iomod);
852	return status;
853}
854
855/* Parse input from a file and execute it */
856
857int
858PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
859		     PyCompilerFlags *flags)
860{
861	if (filename == NULL)
862		filename = "???";
863	if (Py_FdIsInteractive(fp, filename)) {
864		int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
865		if (closeit)
866			fclose(fp);
867		return err;
868	}
869	else
870		return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
871}
872
873int
874PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
875{
876	PyObject *v;
877	int ret;
878	PyCompilerFlags local_flags;
879
880	if (flags == NULL) {
881		flags = &local_flags;
882		local_flags.cf_flags = 0;
883	}
884	v = PySys_GetObject("ps1");
885	if (v == NULL) {
886		PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
887		Py_XDECREF(v);
888	}
889	v = PySys_GetObject("ps2");
890	if (v == NULL) {
891		PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
892		Py_XDECREF(v);
893	}
894	for (;;) {
895		ret = PyRun_InteractiveOneFlags(fp, filename, flags);
896		PRINT_TOTAL_REFS();
897		if (ret == E_EOF)
898			return 0;
899		/*
900		if (ret == E_NOMEM)
901			return -1;
902		*/
903	}
904}
905
906/* compute parser flags based on compiler flags */
907#define PARSER_FLAGS(flags) \
908	((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
909		      PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
910
911#if 0
912/* Keep an example of flags with future keyword support. */
913#define PARSER_FLAGS(flags) \
914	((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
915		      PyPARSE_DONT_IMPLY_DEDENT : 0) \
916		    | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
917		       PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
918#endif
919
920int
921PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
922{
923	PyObject *m, *d, *v, *w, *oenc = NULL;
924	mod_ty mod;
925	PyArena *arena;
926	char *ps1 = "", *ps2 = "", *enc = NULL;
927	int errcode = 0;
928
929	if (fp == stdin) {
930		/* Fetch encoding from sys.stdin */
931		v = PySys_GetObject("stdin");
932		if (v == NULL || v == Py_None)
933			return -1;
934		oenc = PyObject_GetAttrString(v, "encoding");
935		if (!oenc)
936			return -1;
937		enc = PyUnicode_AsString(oenc);
938	}
939	v = PySys_GetObject("ps1");
940	if (v != NULL) {
941		v = PyObject_Str(v);
942		if (v == NULL)
943			PyErr_Clear();
944		else if (PyUnicode_Check(v))
945			ps1 = PyUnicode_AsString(v);
946	}
947	w = PySys_GetObject("ps2");
948	if (w != NULL) {
949		w = PyObject_Str(w);
950		if (w == NULL)
951			PyErr_Clear();
952		else if (PyUnicode_Check(w))
953			ps2 = PyUnicode_AsString(w);
954	}
955	arena = PyArena_New();
956	if (arena == NULL) {
957		Py_XDECREF(v);
958		Py_XDECREF(w);
959		Py_XDECREF(oenc);
960		return -1;
961	}
962	mod = PyParser_ASTFromFile(fp, filename, enc,
963				   Py_single_input, ps1, ps2,
964				   flags, &errcode, arena);
965	Py_XDECREF(v);
966	Py_XDECREF(w);
967	Py_XDECREF(oenc);
968	if (mod == NULL) {
969		PyArena_Free(arena);
970		if (errcode == E_EOF) {
971			PyErr_Clear();
972			return E_EOF;
973		}
974		PyErr_Print();
975		return -1;
976	}
977	m = PyImport_AddModule("__main__");
978	if (m == NULL) {
979		PyArena_Free(arena);
980		return -1;
981	}
982	d = PyModule_GetDict(m);
983	v = run_mod(mod, filename, d, d, flags, arena);
984	PyArena_Free(arena);
985	if (v == NULL) {
986		PyErr_Print();
987		return -1;
988	}
989	Py_DECREF(v);
990	return 0;
991}
992
993/* Check whether a file maybe a pyc file: Look at the extension,
994   the file type, and, if we may close it, at the first few bytes. */
995
996static int
997maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
998{
999	if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1000		return 1;
1001
1002	/* Only look into the file if we are allowed to close it, since
1003	   it then should also be seekable. */
1004	if (closeit) {
1005		/* Read only two bytes of the magic. If the file was opened in
1006		   text mode, the bytes 3 and 4 of the magic (\r\n) might not
1007		   be read as they are on disk. */
1008		unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1009		unsigned char buf[2];
1010		/* Mess:  In case of -x, the stream is NOT at its start now,
1011		   and ungetc() was used to push back the first newline,
1012		   which makes the current stream position formally undefined,
1013		   and a x-platform nightmare.
1014		   Unfortunately, we have no direct way to know whether -x
1015		   was specified.  So we use a terrible hack:  if the current
1016		   stream position is not 0, we assume -x was specified, and
1017		   give up.  Bug 132850 on SourceForge spells out the
1018		   hopelessness of trying anything else (fseek and ftell
1019		   don't work predictably x-platform for text-mode files).
1020		*/
1021		int ispyc = 0;
1022		if (ftell(fp) == 0) {
1023			if (fread(buf, 1, 2, fp) == 2 &&
1024			    ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1025				ispyc = 1;
1026			rewind(fp);
1027		}
1028		return ispyc;
1029	}
1030	return 0;
1031}
1032
1033int
1034PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1035			PyCompilerFlags *flags)
1036{
1037	PyObject *m, *d, *v;
1038	const char *ext;
1039	int set_file_name = 0, ret;
1040
1041	m = PyImport_AddModule("__main__");
1042	if (m == NULL)
1043		return -1;
1044	d = PyModule_GetDict(m);
1045	if (PyDict_GetItemString(d, "__file__") == NULL) {
1046		PyObject *f;
1047		f = PyUnicode_DecodeFSDefault(filename);
1048		if (f == NULL)
1049			return -1;
1050		if (PyDict_SetItemString(d, "__file__", f) < 0) {
1051			Py_DECREF(f);
1052			return -1;
1053		}
1054		set_file_name = 1;
1055		Py_DECREF(f);
1056	}
1057	ext = filename + strlen(filename) - 4;
1058	if (maybe_pyc_file(fp, filename, ext, closeit)) {
1059		/* Try to run a pyc file. First, re-open in binary */
1060		if (closeit)
1061			fclose(fp);
1062		if ((fp = fopen(filename, "rb")) == NULL) {
1063			fprintf(stderr, "python: Can't reopen .pyc file\n");
1064			ret = -1;
1065			goto done;
1066		}
1067		/* Turn on optimization if a .pyo file is given */
1068		if (strcmp(ext, ".pyo") == 0)
1069			Py_OptimizeFlag = 1;
1070		v = run_pyc_file(fp, filename, d, d, flags);
1071	} else {
1072		v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1073				      closeit, flags);
1074	}
1075	if (v == NULL) {
1076		PyErr_Print();
1077		ret = -1;
1078		goto done;
1079	}
1080	Py_DECREF(v);
1081	ret = 0;
1082  done:
1083	if (set_file_name && PyDict_DelItemString(d, "__file__"))
1084		PyErr_Clear();
1085	return ret;
1086}
1087
1088int
1089PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
1090{
1091	PyObject *m, *d, *v;
1092	m = PyImport_AddModule("__main__");
1093	if (m == NULL)
1094		return -1;
1095	d = PyModule_GetDict(m);
1096	v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1097	if (v == NULL) {
1098		PyErr_Print();
1099		return -1;
1100	}
1101	Py_DECREF(v);
1102	return 0;
1103}
1104
1105static int
1106parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1107		   int *lineno, int *offset, const char **text)
1108{
1109	long hold;
1110	PyObject *v;
1111
1112	/* old style errors */
1113	if (PyTuple_Check(err))
1114		return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1115					lineno, offset, text);
1116
1117	/* new style errors.  `err' is an instance */
1118
1119	if (! (v = PyObject_GetAttrString(err, "msg")))
1120		goto finally;
1121	*message = v;
1122
1123	if (!(v = PyObject_GetAttrString(err, "filename")))
1124		goto finally;
1125	if (v == Py_None)
1126		*filename = NULL;
1127	else if (! (*filename = PyUnicode_AsString(v)))
1128		goto finally;
1129
1130	Py_DECREF(v);
1131	if (!(v = PyObject_GetAttrString(err, "lineno")))
1132		goto finally;
1133	hold = PyLong_AsLong(v);
1134	Py_DECREF(v);
1135	v = NULL;
1136	if (hold < 0 && PyErr_Occurred())
1137		goto finally;
1138	*lineno = (int)hold;
1139
1140	if (!(v = PyObject_GetAttrString(err, "offset")))
1141		goto finally;
1142	if (v == Py_None) {
1143		*offset = -1;
1144		Py_DECREF(v);
1145		v = NULL;
1146	} else {
1147		hold = PyLong_AsLong(v);
1148		Py_DECREF(v);
1149		v = NULL;
1150		if (hold < 0 && PyErr_Occurred())
1151			goto finally;
1152		*offset = (int)hold;
1153	}
1154
1155	if (!(v = PyObject_GetAttrString(err, "text")))
1156		goto finally;
1157	if (v == Py_None)
1158		*text = NULL;
1159        else if (!PyUnicode_Check(v) ||
1160		 !(*text = PyUnicode_AsString(v)))
1161		goto finally;
1162	Py_DECREF(v);
1163	return 1;
1164
1165finally:
1166	Py_XDECREF(v);
1167	return 0;
1168}
1169
1170void
1171PyErr_Print(void)
1172{
1173	PyErr_PrintEx(1);
1174}
1175
1176static void
1177print_error_text(PyObject *f, int offset, const char *text)
1178{
1179	char *nl;
1180	if (offset >= 0) {
1181		if (offset > 0 && offset == (int)strlen(text))
1182			offset--;
1183		for (;;) {
1184			nl = strchr(text, '\n');
1185			if (nl == NULL || nl-text >= offset)
1186				break;
1187			offset -= (int)(nl+1-text);
1188			text = nl+1;
1189		}
1190		while (*text == ' ' || *text == '\t') {
1191			text++;
1192			offset--;
1193		}
1194	}
1195	PyFile_WriteString("    ", f);
1196	PyFile_WriteString(text, f);
1197	if (*text == '\0' || text[strlen(text)-1] != '\n')
1198		PyFile_WriteString("\n", f);
1199	if (offset == -1)
1200		return;
1201	PyFile_WriteString("    ", f);
1202	offset--;
1203	while (offset > 0) {
1204		PyFile_WriteString(" ", f);
1205		offset--;
1206	}
1207	PyFile_WriteString("^\n", f);
1208}
1209
1210static void
1211handle_system_exit(void)
1212{
1213	PyObject *exception, *value, *tb;
1214	int exitcode = 0;
1215
1216	if (Py_InspectFlag)
1217		/* Don't exit if -i flag was given. This flag is set to 0
1218		 * when entering interactive mode for inspecting. */
1219		return;
1220
1221	PyErr_Fetch(&exception, &value, &tb);
1222	fflush(stdout);
1223	if (value == NULL || value == Py_None)
1224		goto done;
1225	if (PyExceptionInstance_Check(value)) {
1226		/* The error code should be in the `code' attribute. */
1227		PyObject *code = PyObject_GetAttrString(value, "code");
1228		if (code) {
1229			Py_DECREF(value);
1230			value = code;
1231			if (value == Py_None)
1232				goto done;
1233		}
1234		/* If we failed to dig out the 'code' attribute,
1235		   just let the else clause below print the error. */
1236	}
1237	if (PyLong_Check(value))
1238		exitcode = (int)PyLong_AsLong(value);
1239	else {
1240		PyObject_Print(value, stderr, Py_PRINT_RAW);
1241		PySys_WriteStderr("\n");
1242		exitcode = 1;
1243	}
1244 done:
1245	/* Restore and clear the exception info, in order to properly decref
1246	 * the exception, value, and traceback.	 If we just exit instead,
1247	 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1248	 * some finalizers from running.
1249	 */
1250	PyErr_Restore(exception, value, tb);
1251	PyErr_Clear();
1252	Py_Exit(exitcode);
1253	/* NOTREACHED */
1254}
1255
1256void
1257PyErr_PrintEx(int set_sys_last_vars)
1258{
1259	PyObject *exception, *v, *tb, *hook;
1260
1261	if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1262		handle_system_exit();
1263	}
1264	PyErr_Fetch(&exception, &v, &tb);
1265	if (exception == NULL)
1266		return;
1267	PyErr_NormalizeException(&exception, &v, &tb);
1268	if (exception == NULL)
1269		return;
1270        /* Now we know v != NULL too */
1271	if (set_sys_last_vars) {
1272		PySys_SetObject("last_type", exception);
1273		PySys_SetObject("last_value", v);
1274		PySys_SetObject("last_traceback", tb ? tb : Py_None);
1275	}
1276	hook = PySys_GetObject("excepthook");
1277	if (hook) {
1278		PyObject *args = PyTuple_Pack(3,
1279		    exception, v, tb ? tb : Py_None);
1280		PyObject *result = PyEval_CallObject(hook, args);
1281		if (result == NULL) {
1282			PyObject *exception2, *v2, *tb2;
1283			if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1284				handle_system_exit();
1285			}
1286			PyErr_Fetch(&exception2, &v2, &tb2);
1287			PyErr_NormalizeException(&exception2, &v2, &tb2);
1288			/* It should not be possible for exception2 or v2
1289			   to be NULL. However PyErr_Display() can't
1290			   tolerate NULLs, so just be safe. */
1291			if (exception2 == NULL) {
1292				exception2 = Py_None;
1293				Py_INCREF(exception2);
1294			}
1295			if (v2 == NULL) {
1296				v2 = Py_None;
1297				Py_INCREF(v2);
1298			}
1299			fflush(stdout);
1300			PySys_WriteStderr("Error in sys.excepthook:\n");
1301			PyErr_Display(exception2, v2, tb2);
1302			PySys_WriteStderr("\nOriginal exception was:\n");
1303			PyErr_Display(exception, v, tb);
1304			Py_DECREF(exception2);
1305			Py_DECREF(v2);
1306			Py_XDECREF(tb2);
1307		}
1308		Py_XDECREF(result);
1309		Py_XDECREF(args);
1310	} else {
1311		PySys_WriteStderr("sys.excepthook is missing\n");
1312		PyErr_Display(exception, v, tb);
1313	}
1314	Py_XDECREF(exception);
1315	Py_XDECREF(v);
1316	Py_XDECREF(tb);
1317}
1318
1319void
1320PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1321{
1322	int err = 0;
1323	PyObject *f = PySys_GetObject("stderr");
1324	Py_INCREF(value);
1325	if (f == Py_None) {
1326		/* pass */
1327	}
1328	else if (f == NULL) {
1329		_PyObject_Dump(value);
1330		fprintf(stderr, "lost sys.stderr\n");
1331	}
1332	else {
1333		fflush(stdout);
1334		if (tb && tb != Py_None)
1335			err = PyTraceBack_Print(tb, f);
1336		if (err == 0 &&
1337		    PyObject_HasAttrString(value, "print_file_and_line"))
1338		{
1339			PyObject *message;
1340			const char *filename, *text;
1341			int lineno, offset;
1342			if (!parse_syntax_error(value, &message, &filename,
1343						&lineno, &offset, &text))
1344				PyErr_Clear();
1345			else {
1346				char buf[10];
1347				PyFile_WriteString("  File \"", f);
1348				if (filename == NULL)
1349					PyFile_WriteString("<string>", f);
1350				else
1351					PyFile_WriteString(filename, f);
1352				PyFile_WriteString("\", line ", f);
1353				PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1354				PyFile_WriteString(buf, f);
1355				PyFile_WriteString("\n", f);
1356				if (text != NULL)
1357					print_error_text(f, offset, text);
1358				Py_DECREF(value);
1359				value = message;
1360				/* Can't be bothered to check all those
1361				   PyFile_WriteString() calls */
1362				if (PyErr_Occurred())
1363					err = -1;
1364			}
1365		}
1366		if (err) {
1367			/* Don't do anything else */
1368		}
1369		else if (PyExceptionClass_Check(exception)) {
1370			PyObject* moduleName;
1371			char* className = PyExceptionClass_Name(exception);
1372			if (className != NULL) {
1373				char *dot = strrchr(className, '.');
1374				if (dot != NULL)
1375					className = dot+1;
1376			}
1377
1378			moduleName = PyObject_GetAttrString(exception, "__module__");
1379			if (moduleName == NULL || !PyUnicode_Check(moduleName))
1380			{
1381				Py_DECREF(moduleName);
1382				err = PyFile_WriteString("<unknown>", f);
1383			}
1384			else {
1385				char* modstr = PyUnicode_AsString(moduleName);
1386				if (modstr && strcmp(modstr, "builtins"))
1387				{
1388					err = PyFile_WriteString(modstr, f);
1389					err += PyFile_WriteString(".", f);
1390				}
1391				Py_DECREF(moduleName);
1392			}
1393			if (err == 0) {
1394				if (className == NULL)
1395				      err = PyFile_WriteString("<unknown>", f);
1396				else
1397				      err = PyFile_WriteString(className, f);
1398			}
1399		}
1400		else
1401			err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1402		if (err == 0 && (value != Py_None)) {
1403			PyObject *s = PyObject_Str(value);
1404			/* only print colon if the str() of the
1405			   object is not the empty string
1406			*/
1407			if (s == NULL)
1408				err = -1;
1409			else if (!PyUnicode_Check(s) ||
1410				 PyUnicode_GetSize(s) != 0)
1411				err = PyFile_WriteString(": ", f);
1412			if (err == 0)
1413			  err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1414			Py_XDECREF(s);
1415		}
1416		/* try to write a newline in any case */
1417		err += PyFile_WriteString("\n", f);
1418	}
1419	Py_DECREF(value);
1420	/* If an error happened here, don't show it.
1421	   XXX This is wrong, but too many callers rely on this behavior. */
1422	if (err != 0)
1423		PyErr_Clear();
1424}
1425
1426PyObject *
1427PyRun_StringFlags(const char *str, int start, PyObject *globals,
1428		  PyObject *locals, PyCompilerFlags *flags)
1429{
1430	PyObject *ret = NULL;
1431	mod_ty mod;
1432	PyArena *arena = PyArena_New();
1433	if (arena == NULL)
1434		return NULL;
1435
1436	mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1437	if (mod != NULL)
1438		ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1439	PyArena_Free(arena);
1440	return ret;
1441}
1442
1443PyObject *
1444PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1445		  PyObject *locals, int closeit, PyCompilerFlags *flags)
1446{
1447	PyObject *ret;
1448	mod_ty mod;
1449	PyArena *arena = PyArena_New();
1450	if (arena == NULL)
1451		return NULL;
1452
1453	mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1454				   flags, NULL, arena);
1455	if (closeit)
1456		fclose(fp);
1457	if (mod == NULL) {
1458		PyArena_Free(arena);
1459		return NULL;
1460	}
1461	ret = run_mod(mod, filename, globals, locals, flags, arena);
1462	PyArena_Free(arena);
1463	return ret;
1464}
1465
1466static void
1467flush_io(void)
1468{
1469	PyObject *f, *r;
1470	PyObject *type, *value, *traceback;
1471
1472	/* Save the current exception */
1473	PyErr_Fetch(&type, &value, &traceback);
1474
1475	f = PySys_GetObject("stderr");
1476	if (f != NULL) {
1477		r = PyObject_CallMethod(f, "flush", "");
1478		if (r)
1479			Py_DECREF(r);
1480		else
1481			PyErr_Clear();
1482	}
1483	f = PySys_GetObject("stdout");
1484	if (f != NULL) {
1485		r = PyObject_CallMethod(f, "flush", "");
1486		if (r)
1487			Py_DECREF(r);
1488		else
1489			PyErr_Clear();
1490	}
1491
1492	PyErr_Restore(type, value, traceback);
1493}
1494
1495static PyObject *
1496run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1497	 PyCompilerFlags *flags, PyArena *arena)
1498{
1499	PyCodeObject *co;
1500	PyObject *v;
1501	co = PyAST_Compile(mod, filename, flags, arena);
1502	if (co == NULL)
1503		return NULL;
1504	v = PyEval_EvalCode(co, globals, locals);
1505	Py_DECREF(co);
1506	flush_io();
1507	return v;
1508}
1509
1510static PyObject *
1511run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1512	     PyObject *locals, PyCompilerFlags *flags)
1513{
1514	PyCodeObject *co;
1515	PyObject *v;
1516	long magic;
1517	long PyImport_GetMagicNumber(void);
1518
1519	magic = PyMarshal_ReadLongFromFile(fp);
1520	if (magic != PyImport_GetMagicNumber()) {
1521		PyErr_SetString(PyExc_RuntimeError,
1522			   "Bad magic number in .pyc file");
1523		return NULL;
1524	}
1525	(void) PyMarshal_ReadLongFromFile(fp);
1526	v = PyMarshal_ReadLastObjectFromFile(fp);
1527	fclose(fp);
1528	if (v == NULL || !PyCode_Check(v)) {
1529		Py_XDECREF(v);
1530		PyErr_SetString(PyExc_RuntimeError,
1531			   "Bad code object in .pyc file");
1532		return NULL;
1533	}
1534	co = (PyCodeObject *)v;
1535	v = PyEval_EvalCode(co, globals, locals);
1536	if (v && flags)
1537		flags->cf_flags |= (co->co_flags & PyCF_MASK);
1538	Py_DECREF(co);
1539	flush_io();
1540	return v;
1541}
1542
1543PyObject *
1544Py_CompileStringFlags(const char *str, const char *filename, int start,
1545		      PyCompilerFlags *flags)
1546{
1547	PyCodeObject *co;
1548	mod_ty mod;
1549	PyArena *arena = PyArena_New();
1550	if (arena == NULL)
1551		return NULL;
1552
1553	mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1554	if (mod == NULL) {
1555		PyArena_Free(arena);
1556		return NULL;
1557	}
1558	if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1559		PyObject *result = PyAST_mod2obj(mod);
1560		PyArena_Free(arena);
1561		return result;
1562	}
1563	co = PyAST_Compile(mod, filename, flags, arena);
1564	PyArena_Free(arena);
1565	return (PyObject *)co;
1566}
1567
1568struct symtable *
1569Py_SymtableString(const char *str, const char *filename, int start)
1570{
1571	struct symtable *st;
1572	mod_ty mod;
1573	PyCompilerFlags flags;
1574	PyArena *arena = PyArena_New();
1575	if (arena == NULL)
1576		return NULL;
1577
1578	flags.cf_flags = 0;
1579	mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1580	if (mod == NULL) {
1581		PyArena_Free(arena);
1582		return NULL;
1583	}
1584	st = PySymtable_Build(mod, filename, 0);
1585	PyArena_Free(arena);
1586	return st;
1587}
1588
1589/* Preferred access to parser is through AST. */
1590mod_ty
1591PyParser_ASTFromString(const char *s, const char *filename, int start,
1592		       PyCompilerFlags *flags, PyArena *arena)
1593{
1594	mod_ty mod;
1595	perrdetail err;
1596	int iflags = PARSER_FLAGS(flags);
1597
1598	node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1599					&_PyParser_Grammar, start, &err,
1600					&iflags);
1601	if (n) {
1602		if (flags) {
1603			flags->cf_flags |= iflags & PyCF_MASK;
1604		}
1605		mod = PyAST_FromNode(n, flags, filename, arena);
1606		PyNode_Free(n);
1607		return mod;
1608	}
1609	else {
1610		err_input(&err);
1611		return NULL;
1612	}
1613}
1614
1615mod_ty
1616PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1617		     int start, char *ps1,
1618		     char *ps2, PyCompilerFlags *flags, int *errcode,
1619		     PyArena *arena)
1620{
1621	mod_ty mod;
1622	perrdetail err;
1623	int iflags = PARSER_FLAGS(flags);
1624
1625	node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1626					  &_PyParser_Grammar,
1627				start, ps1, ps2, &err, &iflags);
1628	if (n) {
1629		if (flags) {
1630			flags->cf_flags |= iflags & PyCF_MASK;
1631		}
1632		mod = PyAST_FromNode(n, flags, filename, arena);
1633		PyNode_Free(n);
1634		return mod;
1635	}
1636	else {
1637		err_input(&err);
1638		if (errcode)
1639			*errcode = err.error;
1640		return NULL;
1641	}
1642}
1643
1644/* Simplified interface to parsefile -- return node or set exception */
1645
1646node *
1647PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1648{
1649	perrdetail err;
1650	node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1651					  &_PyParser_Grammar,
1652					  start, NULL, NULL, &err, flags);
1653	if (n == NULL)
1654		err_input(&err);
1655
1656	return n;
1657}
1658
1659/* Simplified interface to parsestring -- return node or set exception */
1660
1661node *
1662PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1663{
1664	perrdetail err;
1665	node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1666					    start, &err, flags);
1667	if (n == NULL)
1668		err_input(&err);
1669	return n;
1670}
1671
1672node *
1673PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1674					int start, int flags)
1675{
1676	perrdetail err;
1677	node *n = PyParser_ParseStringFlagsFilename(str, filename,
1678				&_PyParser_Grammar, start, &err, flags);
1679	if (n == NULL)
1680		err_input(&err);
1681	return n;
1682}
1683
1684node *
1685PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1686{
1687	return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1688}
1689
1690/* May want to move a more generalized form of this to parsetok.c or
1691   even parser modules. */
1692
1693void
1694PyParser_SetError(perrdetail *err)
1695{
1696	err_input(err);
1697}
1698
1699/* Set the error appropriate to the given input error code (see errcode.h) */
1700
1701static void
1702err_input(perrdetail *err)
1703{
1704	PyObject *v, *w, *errtype, *errtext;
1705	PyObject* u = NULL;
1706	char *msg = NULL;
1707	errtype = PyExc_SyntaxError;
1708	switch (err->error) {
1709	case E_SYNTAX:
1710		errtype = PyExc_IndentationError;
1711		if (err->expected == INDENT)
1712			msg = "expected an indented block";
1713		else if (err->token == INDENT)
1714			msg = "unexpected indent";
1715		else if (err->token == DEDENT)
1716			msg = "unexpected unindent";
1717		else {
1718			errtype = PyExc_SyntaxError;
1719			msg = "invalid syntax";
1720		}
1721		break;
1722	case E_TOKEN:
1723		msg = "invalid token";
1724		break;
1725	case E_EOFS:
1726		msg = "EOF while scanning triple-quoted string";
1727		break;
1728	case E_EOLS:
1729		msg = "EOL while scanning single-quoted string";
1730		break;
1731	case E_INTR:
1732		if (!PyErr_Occurred())
1733			PyErr_SetNone(PyExc_KeyboardInterrupt);
1734		return;
1735	case E_NOMEM:
1736		PyErr_NoMemory();
1737		return;
1738	case E_EOF:
1739		msg = "unexpected EOF while parsing";
1740		break;
1741	case E_TABSPACE:
1742		errtype = PyExc_TabError;
1743		msg = "inconsistent use of tabs and spaces in indentation";
1744		break;
1745	case E_OVERFLOW:
1746		msg = "expression too long";
1747		break;
1748	case E_DEDENT:
1749		errtype = PyExc_IndentationError;
1750		msg = "unindent does not match any outer indentation level";
1751		break;
1752	case E_TOODEEP:
1753		errtype = PyExc_IndentationError;
1754		msg = "too many levels of indentation";
1755		break;
1756	case E_DECODE: {
1757		PyObject *type, *value, *tb;
1758		PyErr_Fetch(&type, &value, &tb);
1759		if (value != NULL) {
1760			u = PyObject_Str(value);
1761			if (u != NULL) {
1762				msg = PyUnicode_AsString(u);
1763			}
1764		}
1765		if (msg == NULL)
1766			msg = "unknown decode error";
1767		Py_XDECREF(type);
1768		Py_XDECREF(value);
1769		Py_XDECREF(tb);
1770		break;
1771	}
1772	case E_LINECONT:
1773		msg = "unexpected character after line continuation character";
1774		break;
1775
1776	case E_IDENTIFIER:
1777		msg = "invalid character in identifier";
1778		break;
1779	default:
1780		fprintf(stderr, "error=%d\n", err->error);
1781		msg = "unknown parsing error";
1782		break;
1783	}
1784	/* err->text may not be UTF-8 in case of decoding errors.
1785	   Explicitly convert to an object. */
1786	if (!err->text) {
1787		errtext = Py_None;
1788		Py_INCREF(Py_None);
1789	} else {
1790		errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
1791					       "replace");
1792	}
1793	v = Py_BuildValue("(ziiN)", err->filename,
1794			  err->lineno, err->offset, errtext);
1795	if (err->text != NULL) {
1796		PyObject_FREE(err->text);
1797		err->text = NULL;
1798	}
1799	w = NULL;
1800	if (v != NULL)
1801		w = Py_BuildValue("(sO)", msg, v);
1802	Py_XDECREF(u);
1803	Py_XDECREF(v);
1804	PyErr_SetObject(errtype, w);
1805	Py_XDECREF(w);
1806}
1807
1808/* Print fatal error message and abort */
1809
1810void
1811Py_FatalError(const char *msg)
1812{
1813	fprintf(stderr, "Fatal Python error: %s\n", msg);
1814	if (PyErr_Occurred()) {
1815		PyErr_Print();
1816	}
1817#ifdef MS_WINDOWS
1818	OutputDebugString("Fatal Python error: ");
1819	OutputDebugString(msg);
1820	OutputDebugString("\n");
1821#ifdef _DEBUG
1822	DebugBreak();
1823#endif
1824#endif /* MS_WINDOWS */
1825	abort();
1826}
1827
1828/* Clean up and exit */
1829
1830#ifdef WITH_THREAD
1831#include "pythread.h"
1832#endif
1833
1834static void (*pyexitfunc)(void) = NULL;
1835/* For the atexit module. */
1836void _Py_PyAtExit(void (*func)(void))
1837{
1838	pyexitfunc = func;
1839}
1840
1841static void
1842call_py_exitfuncs(void)
1843{
1844	if (pyexitfunc == NULL)
1845		return;
1846
1847	(*pyexitfunc)();
1848	PyErr_Clear();
1849}
1850
1851#define NEXITFUNCS 32
1852static void (*exitfuncs[NEXITFUNCS])(void);
1853static int nexitfuncs = 0;
1854
1855int Py_AtExit(void (*func)(void))
1856{
1857	if (nexitfuncs >= NEXITFUNCS)
1858		return -1;
1859	exitfuncs[nexitfuncs++] = func;
1860	return 0;
1861}
1862
1863static void
1864call_ll_exitfuncs(void)
1865{
1866	while (nexitfuncs > 0)
1867		(*exitfuncs[--nexitfuncs])();
1868
1869	fflush(stdout);
1870	fflush(stderr);
1871}
1872
1873void
1874Py_Exit(int sts)
1875{
1876	Py_Finalize();
1877
1878	exit(sts);
1879}
1880
1881static void
1882initsigs(void)
1883{
1884#ifdef SIGPIPE
1885	PyOS_setsig(SIGPIPE, SIG_IGN);
1886#endif
1887#ifdef SIGXFZ
1888	PyOS_setsig(SIGXFZ, SIG_IGN);
1889#endif
1890#ifdef SIGXFSZ
1891	PyOS_setsig(SIGXFSZ, SIG_IGN);
1892#endif
1893	PyOS_InitInterrupts(); /* May imply initsignal() */
1894}
1895
1896
1897/*
1898 * The file descriptor fd is considered ``interactive'' if either
1899 *   a) isatty(fd) is TRUE, or
1900 *   b) the -i flag was given, and the filename associated with
1901 *      the descriptor is NULL or "<stdin>" or "???".
1902 */
1903int
1904Py_FdIsInteractive(FILE *fp, const char *filename)
1905{
1906	if (isatty((int)fileno(fp)))
1907		return 1;
1908	if (!Py_InteractiveFlag)
1909		return 0;
1910	return (filename == NULL) ||
1911	       (strcmp(filename, "<stdin>") == 0) ||
1912	       (strcmp(filename, "???") == 0);
1913}
1914
1915
1916#if defined(USE_STACKCHECK)
1917#if defined(WIN32) && defined(_MSC_VER)
1918
1919/* Stack checking for Microsoft C */
1920
1921#include <malloc.h>
1922#include <excpt.h>
1923
1924/*
1925 * Return non-zero when we run out of memory on the stack; zero otherwise.
1926 */
1927int
1928PyOS_CheckStack(void)
1929{
1930	__try {
1931		/* alloca throws a stack overflow exception if there's
1932		   not enough space left on the stack */
1933		alloca(PYOS_STACK_MARGIN * sizeof(void*));
1934		return 0;
1935	} __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1936			EXCEPTION_EXECUTE_HANDLER :
1937			EXCEPTION_CONTINUE_SEARCH) {
1938		int errcode = _resetstkoflw();
1939		if (errcode)
1940		{
1941			Py_FatalError("Could not reset the stack!");
1942		}
1943	}
1944	return 1;
1945}
1946
1947#endif /* WIN32 && _MSC_VER */
1948
1949/* Alternate implementations can be added here... */
1950
1951#endif /* USE_STACKCHECK */
1952
1953
1954/* Wrappers around sigaction() or signal(). */
1955
1956PyOS_sighandler_t
1957PyOS_getsig(int sig)
1958{
1959#ifdef HAVE_SIGACTION
1960	struct sigaction context;
1961	if (sigaction(sig, NULL, &context) == -1)
1962		return SIG_ERR;
1963	return context.sa_handler;
1964#else
1965	PyOS_sighandler_t handler;
1966/* Special signal handling for the secure CRT in Visual Studio 2005 */
1967#if defined(_MSC_VER) && _MSC_VER >= 1400
1968	switch (sig) {
1969	/* Only these signals are valid */
1970	case SIGINT:
1971	case SIGILL:
1972	case SIGFPE:
1973	case SIGSEGV:
1974	case SIGTERM:
1975	case SIGBREAK:
1976	case SIGABRT:
1977		break;
1978	/* Don't call signal() with other values or it will assert */
1979	default:
1980		return SIG_ERR;
1981	}
1982#endif /* _MSC_VER && _MSC_VER >= 1400 */
1983	handler = signal(sig, SIG_IGN);
1984	if (handler != SIG_ERR)
1985		signal(sig, handler);
1986	return handler;
1987#endif
1988}
1989
1990PyOS_sighandler_t
1991PyOS_setsig(int sig, PyOS_sighandler_t handler)
1992{
1993#ifdef HAVE_SIGACTION
1994	struct sigaction context, ocontext;
1995	context.sa_handler = handler;
1996	sigemptyset(&context.sa_mask);
1997	context.sa_flags = 0;
1998	if (sigaction(sig, &context, &ocontext) == -1)
1999		return SIG_ERR;
2000	return ocontext.sa_handler;
2001#else
2002	PyOS_sighandler_t oldhandler;
2003	oldhandler = signal(sig, handler);
2004#ifdef HAVE_SIGINTERRUPT
2005	siginterrupt(sig, 1);
2006#endif
2007	return oldhandler;
2008#endif
2009}
2010
2011/* Deprecated C API functions still provided for binary compatiblity */
2012
2013#undef PyParser_SimpleParseFile
2014PyAPI_FUNC(node *)
2015PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
2016{
2017	return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
2018}
2019
2020#undef PyParser_SimpleParseString
2021PyAPI_FUNC(node *)
2022PyParser_SimpleParseString(const char *str, int start)
2023{
2024	return PyParser_SimpleParseStringFlags(str, start, 0);
2025}
2026
2027#undef PyRun_AnyFile
2028PyAPI_FUNC(int)
2029PyRun_AnyFile(FILE *fp, const char *name)
2030{
2031	return PyRun_AnyFileExFlags(fp, name, 0, NULL);
2032}
2033
2034#undef PyRun_AnyFileEx
2035PyAPI_FUNC(int)
2036PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
2037{
2038	return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
2039}
2040
2041#undef PyRun_AnyFileFlags
2042PyAPI_FUNC(int)
2043PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
2044{
2045	return PyRun_AnyFileExFlags(fp, name, 0, flags);
2046}
2047
2048#undef PyRun_File
2049PyAPI_FUNC(PyObject *)
2050PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
2051{
2052        return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
2053}
2054
2055#undef PyRun_FileEx
2056PyAPI_FUNC(PyObject *)
2057PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
2058{
2059        return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
2060}
2061
2062#undef PyRun_FileFlags
2063PyAPI_FUNC(PyObject *)
2064PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
2065		PyCompilerFlags *flags)
2066{
2067        return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
2068}
2069
2070#undef PyRun_SimpleFile
2071PyAPI_FUNC(int)
2072PyRun_SimpleFile(FILE *f, const char *p)
2073{
2074	return PyRun_SimpleFileExFlags(f, p, 0, NULL);
2075}
2076
2077#undef PyRun_SimpleFileEx
2078PyAPI_FUNC(int)
2079PyRun_SimpleFileEx(FILE *f, const char *p, int c)
2080{
2081	return PyRun_SimpleFileExFlags(f, p, c, NULL);
2082}
2083
2084
2085#undef PyRun_String
2086PyAPI_FUNC(PyObject *)
2087PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
2088{
2089	return PyRun_StringFlags(str, s, g, l, NULL);
2090}
2091
2092#undef PyRun_SimpleString
2093PyAPI_FUNC(int)
2094PyRun_SimpleString(const char *s)
2095{
2096	return PyRun_SimpleStringFlags(s, NULL);
2097}
2098
2099#undef Py_CompileString
2100PyAPI_FUNC(PyObject *)
2101Py_CompileString(const char *str, const char *p, int s)
2102{
2103	return Py_CompileStringFlags(str, p, s, NULL);
2104}
2105
2106#undef PyRun_InteractiveOne
2107PyAPI_FUNC(int)
2108PyRun_InteractiveOne(FILE *f, const char *p)
2109{
2110	return PyRun_InteractiveOneFlags(f, p, NULL);
2111}
2112
2113#undef PyRun_InteractiveLoop
2114PyAPI_FUNC(int)
2115PyRun_InteractiveLoop(FILE *f, const char *p)
2116{
2117	return PyRun_InteractiveLoopFlags(f, p, NULL);
2118}
2119
2120#ifdef __cplusplus
2121}
2122#endif
2123