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