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