LCOV - code coverage report
Current view: top level - Python - pythonrun.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 291 900 32.3 %
Date: 2017-04-19 Functions: 27 66 40.9 %

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

Generated by: LCOV version 1.10