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