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