Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 1 | |
| 2 | /* Python interpreter main program for frozen scripts */ |
| 3 | |
Guido van Rossum | 4e2e0f9 | 1995-03-31 10:27:23 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 5 | #include <locale.h> |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 6 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 7 | #ifdef MS_WINDOWS |
Thomas Wouters | b4bd21c | 2000-07-22 23:38:01 +0000 | [diff] [blame] | 8 | extern void PyWinFreeze_ExeInit(void); |
| 9 | extern void PyWinFreeze_ExeTerm(void); |
| 10 | extern int PyInitFrozenExtensions(void); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 11 | #endif |
| 12 | |
Guido van Rossum | 47ad5e7 | 1995-08-04 04:10:43 +0000 | [diff] [blame] | 13 | /* Main program */ |
| 14 | |
| 15 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 16 | Py_FrozenMain(int argc, char **argv) |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 17 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | char *p; |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 19 | int i, n, sts = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | int inspect = 0; |
| 21 | int unbuffered = 0; |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 22 | char *oldloc = NULL; |
| 23 | wchar_t **argv_copy = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | /* We need a second copies, as Python might modify the first one. */ |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 25 | wchar_t **argv_copy2 = NULL; |
Victor Stinner | 36577e4 | 2013-07-27 01:04:56 +0200 | [diff] [blame] | 26 | |
Benjamin Peterson | 2476b98 | 2015-02-14 15:16:32 -0500 | [diff] [blame] | 27 | if (argc > 0) { |
| 28 | argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc); |
| 29 | argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc); |
| 30 | if (!argv_copy || !argv_copy2) { |
| 31 | fprintf(stderr, "out of memory\n"); |
| 32 | goto error; |
| 33 | } |
Victor Stinner | 36577e4 | 2013-07-27 01:04:56 +0200 | [diff] [blame] | 34 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 35 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 36 | Py_FrozenFlag = 1; /* Suppress errors from getpath.c */ |
Guido van Rossum | 919b83d | 1998-02-06 22:30:29 +0000 | [diff] [blame] | 37 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') |
| 39 | inspect = 1; |
| 40 | if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') |
| 41 | unbuffered = 1; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 42 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | if (unbuffered) { |
| 44 | setbuf(stdin, (char *)NULL); |
| 45 | setbuf(stdout, (char *)NULL); |
| 46 | setbuf(stderr, (char *)NULL); |
| 47 | } |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 48 | |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 49 | oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL)); |
| 50 | if (!oldloc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | fprintf(stderr, "out of memory\n"); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 52 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | setlocale(LC_ALL, ""); |
| 56 | for (i = 0; i < argc; i++) { |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 57 | argv_copy[i] = Py_DecodeLocale(argv[i], NULL); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 58 | argv_copy2[i] = argv_copy[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | if (!argv_copy[i]) { |
Victor Stinner | 739cf4e | 2013-07-27 02:24:52 +0200 | [diff] [blame] | 60 | fprintf(stderr, "Unable to decode the command line argument #%i\n", |
| 61 | i + 1); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 62 | argc = i; |
| 63 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | } |
| 66 | setlocale(LC_ALL, oldloc); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 67 | PyMem_RawFree(oldloc); |
| 68 | oldloc = NULL; |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 69 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 70 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | PyInitFrozenExtensions(); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 72 | #endif /* MS_WINDOWS */ |
Benjamin Peterson | 2476b98 | 2015-02-14 15:16:32 -0500 | [diff] [blame] | 73 | if (argc >= 1) |
| 74 | Py_SetProgramName(argv_copy[0]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | Py_Initialize(); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 76 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | PyWinFreeze_ExeInit(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 78 | #endif |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | if (Py_VerboseFlag) |
| 81 | fprintf(stderr, "Python %s\n%s\n", |
| 82 | Py_GetVersion(), Py_GetCopyright()); |
Guido van Rossum | 3768fb1 | 1997-07-19 19:24:41 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | PySys_SetArgv(argc, argv_copy); |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 86 | n = PyImport_ImportFrozenModule("__main__"); |
| 87 | if (n == 0) |
| 88 | Py_FatalError("__main__ not frozen"); |
| 89 | if (n < 0) { |
| 90 | PyErr_Print(); |
| 91 | sts = 1; |
| 92 | } |
| 93 | else |
| 94 | sts = 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (inspect && isatty((int)fileno(stdin))) |
| 97 | sts = PyRun_AnyFile(stdin, "<stdin>") != 0; |
Guido van Rossum | 9e90a67 | 1993-06-24 11:10:19 +0000 | [diff] [blame] | 98 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 99 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | PyWinFreeze_ExeTerm(); |
Guido van Rossum | 6deac7a | 1998-04-03 21:11:15 +0000 | [diff] [blame] | 101 | #endif |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 102 | if (Py_FinalizeEx() < 0) { |
| 103 | sts = 120; |
| 104 | } |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 105 | |
| 106 | error: |
Victor Stinner | b5245be | 2013-07-27 01:13:34 +0200 | [diff] [blame] | 107 | PyMem_RawFree(argv_copy); |
Victor Stinner | c588fee | 2013-07-27 02:39:09 +0200 | [diff] [blame] | 108 | if (argv_copy2) { |
| 109 | for (i = 0; i < argc; i++) |
| 110 | PyMem_RawFree(argv_copy2[i]); |
| 111 | PyMem_RawFree(argv_copy2); |
| 112 | } |
| 113 | PyMem_RawFree(oldloc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | return sts; |
Guido van Rossum | f56e3db | 1993-04-01 20:59:32 +0000 | [diff] [blame] | 115 | } |