Home | History | Annotate | Download | only in Python
      1 
      2 /* Python interpreter main program for frozen scripts */
      3 
      4 #include "Python.h"
      5 #include "internal/pystate.h"
      6 #include <locale.h>
      7 
      8 #ifdef MS_WINDOWS
      9 extern void PyWinFreeze_ExeInit(void);
     10 extern void PyWinFreeze_ExeTerm(void);
     11 extern int PyInitFrozenExtensions(void);
     12 #endif
     13 
     14 /* Main program */
     15 
     16 int
     17 Py_FrozenMain(int argc, char **argv)
     18 {
     19     _PyInitError err = _PyRuntime_Initialize();
     20     if (_Py_INIT_FAILED(err)) {
     21         fprintf(stderr, "Fatal Python error: %s\n", err.msg);
     22         fflush(stderr);
     23         exit(1);
     24     }
     25 
     26     const char *p;
     27     int i, n, sts = 1;
     28     int inspect = 0;
     29     int unbuffered = 0;
     30     char *oldloc = NULL;
     31     wchar_t **argv_copy = NULL;
     32     /* We need a second copies, as Python might modify the first one. */
     33     wchar_t **argv_copy2 = NULL;
     34 
     35     if (argc > 0) {
     36         argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
     37         argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
     38         if (!argv_copy || !argv_copy2) {
     39             fprintf(stderr, "out of memory\n");
     40             goto error;
     41         }
     42     }
     43 
     44     Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
     45 
     46     if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
     47         inspect = 1;
     48     if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
     49         unbuffered = 1;
     50 
     51     if (unbuffered) {
     52         setbuf(stdin, (char *)NULL);
     53         setbuf(stdout, (char *)NULL);
     54         setbuf(stderr, (char *)NULL);
     55     }
     56 
     57     oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
     58     if (!oldloc) {
     59         fprintf(stderr, "out of memory\n");
     60         goto error;
     61     }
     62 
     63     setlocale(LC_ALL, "");
     64     for (i = 0; i < argc; i++) {
     65         argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
     66         argv_copy2[i] = argv_copy[i];
     67         if (!argv_copy[i]) {
     68             fprintf(stderr, "Unable to decode the command line argument #%i\n",
     69                             i + 1);
     70             argc = i;
     71             goto error;
     72         }
     73     }
     74     setlocale(LC_ALL, oldloc);
     75     PyMem_RawFree(oldloc);
     76     oldloc = NULL;
     77 
     78 #ifdef MS_WINDOWS
     79     PyInitFrozenExtensions();
     80 #endif /* MS_WINDOWS */
     81     if (argc >= 1)
     82         Py_SetProgramName(argv_copy[0]);
     83     Py_Initialize();
     84 #ifdef MS_WINDOWS
     85     PyWinFreeze_ExeInit();
     86 #endif
     87 
     88     if (Py_VerboseFlag)
     89         fprintf(stderr, "Python %s\n%s\n",
     90             Py_GetVersion(), Py_GetCopyright());
     91 
     92     PySys_SetArgv(argc, argv_copy);
     93 
     94     n = PyImport_ImportFrozenModule("__main__");
     95     if (n == 0)
     96         Py_FatalError("__main__ not frozen");
     97     if (n < 0) {
     98         PyErr_Print();
     99         sts = 1;
    100     }
    101     else
    102         sts = 0;
    103 
    104     if (inspect && isatty((int)fileno(stdin)))
    105         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
    106 
    107 #ifdef MS_WINDOWS
    108     PyWinFreeze_ExeTerm();
    109 #endif
    110     if (Py_FinalizeEx() < 0) {
    111         sts = 120;
    112     }
    113 
    114 error:
    115     PyMem_RawFree(argv_copy);
    116     if (argv_copy2) {
    117         for (i = 0; i < argc; i++)
    118             PyMem_RawFree(argv_copy2[i]);
    119         PyMem_RawFree(argv_copy2);
    120     }
    121     PyMem_RawFree(oldloc);
    122     return sts;
    123 }
    124