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